﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Data_DAO_DTO_Lib
{
    public interface DAO
    {
        DTO.DTO LayTuID(int ID);

        DTO.DTO Them(DTO.DTO ob);

        void Xoa(int id);

        void CapNhat(DTO.DTO ob);
        int LaySoLuong();
        int LaySoLuong(bool isXoa);
        List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting);
        List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa);
        List<DTO.DTO> LayDanhSach();
    }

    public static class DAOUtil
    {
        private static DataClasses1DataContext context = new DataClasses1DataContext();
        public static DataClasses1DataContext layContext()
        {
            return context;
        }
    }

    public class CANBANLIENQUAN_DAO:DAO
    {
        public DTO.DTO Them(DTO.DTO x)
        {
            DTO.CANBANLIENQUAN_DTO ob = (DTO.CANBANLIENQUAN_DTO)x;
            CANBANLIENQUAN i = ob.toObjectDatabase();

            DAOUtil.layContext().CANBANLIENQUANs.InsertOnSubmit( i );
            DAOUtil.layContext().SubmitChanges();

            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            CANBANLIENQUAN cb = DAOUtil.layContext().CANBANLIENQUANs.FirstOrDefault(a => a.ID == id);
            cb.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO x)
        {
            DTO.CANBANLIENQUAN_DTO ob = (DTO.CANBANLIENQUAN_DTO)x;
            CANBANLIENQUAN cb = DAOUtil.layContext().CANBANLIENQUANs.FirstOrDefault(a => a.ID == ob.ID);
            cb.IDCanBan = ob.IDCanBan;
            cb.IDCanBanLienQuan = ob.IDCanBanLienQuan;
            cb.Xoa = ob.Xoa;

            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO>  kq =new List<DTO.DTO>();
            List<CANBANLIENQUAN> cb = DAOUtil.layContext().CANBANLIENQUANs.ToList<CANBANLIENQUAN>();
            for (int i = 0; i < cb.Count; i++)
			{
                DTO.CANBANLIENQUAN_DTO cblq = new DTO.CANBANLIENQUAN_DTO();
                cblq.fromObjectDatabase(cb[i]);
                kq.Add(cblq);
			}
            return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
            CANBANLIENQUAN x = DAOUtil.layContext().CANBANLIENQUANs.FirstOrDefault(a => a.ID == ID);
            DTO.CANBANLIENQUAN_DTO ob = new DTO.CANBANLIENQUAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class XUATXUDETAI_DAO : DAO
    {

        public DTO.DTO LayTuID(int ID)
        {
            XUATXUDETAI x = DAOUtil.layContext().XUATXUDETAIs.FirstOrDefault(a => a.ID == ID);
            DTO.XUATXUDETAI_DTO ob = new DTO.XUATXUDETAI_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO x)
        {
            DTO.XUATXUDETAI_DTO ob = (DTO.XUATXUDETAI_DTO)x;
            XUATXUDETAI i = ob.toObjectDatabase();
            DAOUtil.layContext().XUATXUDETAIs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;

            
        }

        public void Xoa(int id)
        {
            XUATXUDETAI cb = DAOUtil.layContext().XUATXUDETAIs.FirstOrDefault(a => a.ID == id);
            cb.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO x)
        {
            DTO.XUATXUDETAI_DTO ob = (DTO.XUATXUDETAI_DTO)x;
            XUATXUDETAI cb = DAOUtil.layContext().XUATXUDETAIs.FirstOrDefault(a => a.ID == ob.ID);
            cb.Ten = ob.Ten;
            cb.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<XUATXUDETAI> cb = DAOUtil.layContext().XUATXUDETAIs.ToList<XUATXUDETAI>();
            for (int i = 0; i < cb.Count; i++)
            {
                DTO.XUATXUDETAI_DTO cblq = new DTO.XUATXUDETAI_DTO();
                cblq.fromObjectDatabase(cb[i]);
                kq.Add(cblq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCuaChaoBan(int IDChaoBan)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();

            THONGTINTHIETBICONGNGHE ttTBCN = DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.Where(e => e.IDCanBan == IDChaoBan).FirstOrDefault();
            if (ttTBCN == null || ttTBCN.IDXuatXuDeTai == null)
            {
                return kq;
            }

            DTO.XUATXUDETAI_DTO xlq = new DTO.XUATXUDETAI_DTO();
            xlq.fromObjectDatabase(ttTBCN.XUATXUDETAI);
            kq.Add(xlq);
            return kq;
        }
    }

    public class CHUYENGIAO_DAO : DAO
    {

        public DTO.DTO LayTuID(int ID)
        {
            CHUYENGIAO x = DAOUtil.layContext().CHUYENGIAOs.FirstOrDefault(a => a.ID == ID);
            DTO.CHUYENGIAO_DTO ob = new DTO.CHUYENGIAO_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO x)
        {
            DTO.CHUYENGIAO_DTO ob = (DTO.CHUYENGIAO_DTO)x;
            CHUYENGIAO i = ob.toObjectDatabase();
            DAOUtil.layContext().CHUYENGIAOs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            CHUYENGIAO cb = DAOUtil.layContext().CHUYENGIAOs.FirstOrDefault(a => a.ID == id);
            cb.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO x)
        {
            DTO.CHUYENGIAO_DTO ob = (DTO.CHUYENGIAO_DTO)x;
            CHUYENGIAO cb = DAOUtil.layContext().CHUYENGIAOs.FirstOrDefault(a => a.ID == ob.ID);
            cb.Ten = ob.Ten;
            cb.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<CHUYENGIAO> x = DAOUtil.layContext().CHUYENGIAOs.ToList<CHUYENGIAO>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.CHUYENGIAO_DTO xlq = new DTO.CHUYENGIAO_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCuaChaoBan(int IDChaoBan)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();

            THONGTINCANBAN tt = DAOUtil.layContext().THONGTINCANBANs.Where(e => e.ID == IDChaoBan).FirstOrDefault();

            if (tt == null || tt.IDChuyenGiao == null)
            {
                return kq;
            }
            THONGTINCHUYENGIAOCANBAN ttcg = tt.THONGTINCHUYENGIAOCANBAN;

            List<CHUYENGIAO> lCG = ttcg.DANHSACHCHUYENGIAOCANBANs.Where(e => e.Xoa == false).Select(e => e.CHUYENGIAO).ToList();
            for (int i = 0; i < lCG.Count; i++)
            {
                DTO.CHUYENGIAO_DTO xlq = new DTO.CHUYENGIAO_DTO();
                xlq.fromObjectDatabase(lCG[i]);
                kq.Add(xlq);    
            }
            
            return kq;
        }
    }

    public class CUNGCAP_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            CUNGCAP x = DAOUtil.layContext().CUNGCAPs.FirstOrDefault(a => a.ID == ID);
            DTO.CUNGCAP_DTO ob = new DTO.CUNGCAP_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.CUNGCAP_DTO ob = (DTO.CUNGCAP_DTO)d;
            CUNGCAP i = ob.toObjectDatabase();
            DAOUtil.layContext().CUNGCAPs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            CUNGCAP x = DAOUtil.layContext().CUNGCAPs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.CUNGCAP_DTO ob = (DTO.CUNGCAP_DTO)d;
            CUNGCAP x = DAOUtil.layContext().CUNGCAPs.FirstOrDefault(a => a.ID == ob.ID);
            x.Ten = ob.Ten;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<CUNGCAP> x = DAOUtil.layContext().CUNGCAPs.ToList<CUNGCAP>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.CUNGCAP_DTO xlq = new DTO.CUNGCAP_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCuaChaoBan(int IDChaoBan)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();

            THONGTINCANBAN tt = DAOUtil.layContext().THONGTINCANBANs.Where(e => e.ID == IDChaoBan).FirstOrDefault();

            if (tt == null || tt.IDChuyenGiao == null)
            {
                return kq;
            }
            THONGTINCHUYENGIAOCANBAN ttcg = tt.THONGTINCHUYENGIAOCANBAN;

            List<CUNGCAP> lCG = ttcg.DANHSACHCUNGCAPCANBANs.Where(e => e.Xoa == false).Select(e => e.CUNGCAP).ToList();
            for (int i = 0; i < lCG.Count; i++)
            {
                DTO.CUNGCAP_DTO xlq = new DTO.CUNGCAP_DTO();
                xlq.fromObjectDatabase(lCG[i]);
                kq.Add(xlq);
            }

            return kq;
        }
    }

    public partial class DANHMUC_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHMUC x = DAOUtil.layContext().DANHMUCs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHMUC_DTO ob = new DTO.DANHMUC_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHMUC_DTO ob = (DTO.DANHMUC_DTO)d;
            DANHMUC i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHMUCs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {

            DANHMUC x = DAOUtil.layContext().DANHMUCs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHMUC_DTO ob = (DTO.DANHMUC_DTO)d;
            DANHMUC x = DAOUtil.layContext().DANHMUCs.FirstOrDefault(a => a.ID == ob.ID);
            x.TenDanhMuc = ob.TenDanhMuc;
            x.SoLuongTrongDanhMuc = ob.SoLuongTrongDanhMuc;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUC> x = DAOUtil.layContext().DANHMUCs.ToList<DANHMUC>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUC_DTO xlq = new DTO.DANHMUC_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachCoCanBan()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUC> x = DAOUtil.layContext().DANHMUCs.Where(e => e.DANHMUCANBANs.Count > 0).ToList();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUC_DTO xlq = new DTO.DANHMUC_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().DANHMUCs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            
            IEnumerable<DANHMUC> query = DAOUtil.layContext().DANHMUCs.AsEnumerable();

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("TenDanhMuc DESC"))
            {
                query = query.OrderByDescending(p => p.TenDanhMuc);
            }
            else if (sorting.Equals("TenDanhMuc ASC"))
            {
                query = query.OrderBy(p => p.TenDanhMuc);
            }
            else if (sorting.Equals("SoLuongTrongDanhMuc ASC"))
            {
                query = query.OrderBy(p => p.SoLuongTrongDanhMuc);
            }
            else if (sorting.Equals("SoLuongTrongDanhMuc DESC"))
            {
                query = query.OrderByDescending(p => p.SoLuongTrongDanhMuc);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else
            {
                //m?c d?nh theo th?i gian
                query = query.OrderByDescending(p => p.TenDanhMuc);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (DANHMUC item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.DANHMUC_DTO rv = new DTO.DANHMUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (DANHMUC item in query.ToList())
                    {
                        DTO.DANHMUC_DTO rv = new DTO.DANHMUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq; 
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCanBanTrongDanhMuc(int id, int startIndex, int count, string sorting)
        {
            IEnumerable<THONGTINCANBAN> query = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e=>e.ID == id).DANHMUCANBANs.Select(e => e.THONGTINCANBAN).AsEnumerable();

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("ID DESC"))
            {
                query = query.OrderByDescending(p => p.ID);
            }
            else if (sorting.Equals("ID ASC"))
            {
                query = query.OrderBy(p => p.ID);
            }
            if(sorting.Equals("LoaiCanBan DESC"))
            {
                query = query.OrderByDescending(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("LoaiCanBan ASC"))
            {
                query = query.OrderBy(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("TenAnh ASC"))
            {
                query = query.OrderBy(p => p.TenAnh);
            }
            else if (sorting.Equals("TenAnh DESC"))
            {
                query = query.OrderByDescending(p => p.TenAnh);
            }
            else if (sorting.Equals("TenViet ASC"))
            {
                query = query.OrderBy(p => p.TenViet);
            }
            else if (sorting.Equals("TenViet DESC"))
            {
                query = query.OrderByDescending(p => p.TenViet);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("XuatXu ASC"))
            {
                query = query.OrderBy(p => p.XuatXu);
            }
            else if (sorting.Equals("XuatXu DESC"))
            {
                query = query.OrderByDescending(p => p.XuatXu);
            }
            else
            {
                //mặc định theo ID ngược
                query = query.OrderByDescending(p => p.ID);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGTINCANBAN item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGTINCANBAN_DTO rv = new DTO.THONGTINCANBAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGTINCANBAN item in query.ToList())
                    {
                        DTO.THONGTINCANBAN_DTO rv = new DTO.THONGTINCANBAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq;
        }

        public List<DTO.DTO> LayDanhSachTimMuaTrongDanhMuc(int id, int startIndex, int count, string sorting)
        {
            IEnumerable<THONGTINTIMMUA> query = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == id).DANHMUCCANMUAs.Select(e => e.THONGTINTIMMUA).AsEnumerable();

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("ID DESC"))
            {
                query = query.OrderByDescending(p => p.ID);
            }
            else if (sorting.Equals("ID ASC"))
            {
                query = query.OrderBy(p => p.ID);
            }
            if (sorting.Equals("LoaiCanBan DESC"))
            {
                query = query.OrderByDescending(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("LoaiCanBan ASC"))
            {
                query = query.OrderBy(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("TenAnh ASC"))
            {
                query = query.OrderBy(p => p.TenAnh);
            }
            else if (sorting.Equals("TenAnh DESC"))
            {
                query = query.OrderByDescending(p => p.TenAnh);
            }
            else if (sorting.Equals("TenViet ASC"))
            {
                query = query.OrderBy(p => p.TenViet);
            }
            else if (sorting.Equals("TenViet DESC"))
            {
                query = query.OrderByDescending(p => p.TenViet);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("XuatXu ASC"))
            {
                query = query.OrderBy(p => p.XuatXu);
            }
            else if (sorting.Equals("XuatXu DESC"))
            {
                query = query.OrderByDescending(p => p.XuatXu);
            }
            else
            {
                //mặc định theo ID ngược
                query = query.OrderByDescending(p => p.ID);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGTINTIMMUA item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGTINTIMMUA item in query.ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq;
        }

        public List<DTO.DTO> LayDanhSachCoTimMua()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUC> x = DAOUtil.layContext().DANHMUCs.Where(e => e.DANHMUCCANMUAs.Count > 0).ToList();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUC_DTO xlq = new DTO.DANHMUC_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachDanhMucQuanTamTaiKhoan(int IDTaiKhoan)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUC> x = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.Where(a => a.IDTaiKhoanQuanTam == IDTaiKhoan && a.Xoa == false).Select(e => e.DANHMUC).ToList();
             for (int i = 0; i < x.Count; i++)
             {
                 DTO.DANHMUC_DTO xlq = new DTO.DANHMUC_DTO();
                 xlq.fromObjectDatabase(x[i]);
                 kq.Add(xlq);
             }
             return kq;
        }

        public List<DTO.DTO> LayDanhSachDanhMucQuanTamDN(int IDdoanhNghiep)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUC> x = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.Where(a => a.IDDoanhNghiep == IDdoanhNghiep && a.Xoa == false).Select(e => e.DANHMUC).ToList();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUC_DTO xlq = new DTO.DANHMUC_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachCuaChaoBan(int IDChaoBan)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();

            THONGTINCANBAN tt = DAOUtil.layContext().THONGTINCANBANs.Where(e => e.ID == IDChaoBan).FirstOrDefault();

            if (tt == null || tt.IDChuyenGiao == null)
            {
                return kq;
            }

            List<DANHMUC> lCG = tt.DANHMUCANBANs.Where(e => e.Xoa == false).Select(e => e.DANHMUC).ToList();
            for (int i = 0; i < lCG.Count; i++)
            {
                DTO.DANHMUC_DTO xlq = new DTO.DANHMUC_DTO();
                xlq.fromObjectDatabase(lCG[i]);
                kq.Add(xlq);
            }

            return kq;
        }
    }

    public partial class DANHMUCANBAN_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHMUCANBAN x = DAOUtil.layContext().DANHMUCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHMUCANBAN_DTO ob = new DTO.DANHMUCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHMUCANBAN_DTO ob = (DTO.DANHMUCANBAN_DTO)d;
            DANHMUCANBAN i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHMUCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DANHMUCANBAN x = DAOUtil.layContext().DANHMUCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHMUCANBAN_DTO ob = (DTO.DANHMUCANBAN_DTO)d;
            DANHMUCANBAN x = DAOUtil.layContext().DANHMUCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDCanBan = ob.IDCanBan;
            x.IDDanhMuc = ob.IDDanhMuc;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUCANBAN> x = DAOUtil.layContext().DANHMUCANBANs.ToList<DANHMUCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUCANBAN_DTO xlq = new DTO.DANHMUCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class DANHMUCCANMUA_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHMUCCANMUA x = DAOUtil.layContext().DANHMUCCANMUAs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHMUCCANMUA_DTO ob = new DTO.DANHMUCCANMUA_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHMUCCANMUA_DTO ob = (DTO.DANHMUCCANMUA_DTO)d;
            DANHMUCCANMUA i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHMUCCANMUAs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {

            DANHMUCCANMUA x = DAOUtil.layContext().DANHMUCCANMUAs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHMUCCANMUA_DTO ob = (DTO.DANHMUCCANMUA_DTO)d;
            DANHMUCCANMUA x = DAOUtil.layContext().DANHMUCCANMUAs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDCanMua = ob.IDDanhMuc;
            x.IDDanhMuc = ob.IDDanhMuc;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUCCANMUA> x = DAOUtil.layContext().DANHMUCCANMUAs.ToList<DANHMUCCANMUA>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUCCANMUA_DTO xlq = new DTO.DANHMUCCANMUA_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class DANHMUCDOANHNGHIEP_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            throw new NotImplementedException();
            //var kq = DAOUtil.layContext().DOANHNGHIEPs.Select(e => new { DisplayText = e.TenCuaHang, Value = e.ID }).ToList();
            //return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
           // DANHMUCCANMUA x = DAOUtil.layContext().DANHMUCCANMUAs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHMUCDOANHNGHIEP_DTO ob = new DTO.DANHMUCDOANHNGHIEP_DTO();
            //ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHMUCDOANHNGHIEP_DTO ob = (DTO.DANHMUCDOANHNGHIEP_DTO)d;
            DANHMUCDOANHNGHIEP i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHMUCDOANHNGHIEPs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DANHMUCDOANHNGHIEP x = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;

            DAOUtil.layContext().SubmitChanges();
            
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHMUCDOANHNGHIEP_DTO ob = (DTO.DANHMUCDOANHNGHIEP_DTO)d;

            DANHMUCDOANHNGHIEP x = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDDanhMuc = ob.IDDanhMuc;
            x.IDDoanhNghiep = ob.IDDoanhNghiep;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUCDOANHNGHIEP> x = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.ToList<DANHMUCDOANHNGHIEP>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUCDOANHNGHIEP_DTO xlq = new DTO.DANHMUCDOANHNGHIEP_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }


    public class DANHSACHCANBANQUANTAM_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHSACHCANBANQUANTAM x = DAOUtil.layContext().DANHSACHCANBANQUANTAMs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHSACHCANBANQUANTAM_DTO ob = new DTO.DANHSACHCANBANQUANTAM_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHSACHCANBANQUANTAM_DTO ob = (DTO.DANHSACHCANBANQUANTAM_DTO)d;
            DANHSACHCANBANQUANTAM i = DAOUtil.layContext().DANHSACHCANBANQUANTAMs.FirstOrDefault(e => e.IDThongTinCanBan == ob.IDThongTinCanBan && e.IDTaiKhoanQuanTam == ob.IDTaiKhoanQuanTam);

            if (i != null)
            {
                i.Xoa = false;
            }
            else
            {
                i = ob.toObjectDatabase();
                i.NgayQuanTam = DateTime.Now;
                i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDTaiKhoanQuanTam);
                i.THONGTINCANBAN = DAOUtil.layContext().THONGTINCANBANs.FirstOrDefault(e => e.ID == i.IDThongTinCanBan);
                DAOUtil.layContext().DANHSACHCANBANQUANTAMs.InsertOnSubmit(i);    
            }

            
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {

            DANHSACHCANBANQUANTAM x = DAOUtil.layContext().DANHSACHCANBANQUANTAMs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHSACHCANBANQUANTAM_DTO ob = (DTO.DANHSACHCANBANQUANTAM_DTO)d;
            DANHSACHCANBANQUANTAM x = DAOUtil.layContext().DANHSACHCANBANQUANTAMs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDTaiKhoanQuanTam = ob.IDTaiKhoanQuanTam;
            x.IDThongTinCanBan = ob.IDThongTinCanBan;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHSACHCANBANQUANTAM> x = DAOUtil.layContext().DANHSACHCANBANQUANTAMs.ToList<DANHSACHCANBANQUANTAM>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHSACHCANBANQUANTAM_DTO xlq = new DTO.DANHSACHCANBANQUANTAM_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class DANHSACHCHUYENGIAOCANBAN_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHSACHCHUYENGIAOCANBAN x = DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHSACHCHUYENGIAOCANBAN_DTO ob = new DTO.DANHSACHCHUYENGIAOCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHSACHCHUYENGIAOCANBAN_DTO ob = (DTO.DANHSACHCHUYENGIAOCANBAN_DTO)d;
            DANHSACHCHUYENGIAOCANBAN i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DANHSACHCHUYENGIAOCANBAN x = DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHSACHCHUYENGIAOCANBAN_DTO ob = (DTO.DANHSACHCHUYENGIAOCANBAN_DTO)d;
            DANHSACHCHUYENGIAOCANBAN x = DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDChuyenGiao = ob.IDChuyenGiao;
            x.IDChuyenGiaoCanBan = ob.IDChuyenGiaoCanBan;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHSACHCHUYENGIAOCANBAN> x = DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.ToList<DANHSACHCHUYENGIAOCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHSACHCHUYENGIAOCANBAN_DTO xlq = new DTO.DANHSACHCHUYENGIAOCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class DANHSACHCUNGCAPCANBAN_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHSACHCUNGCAPCANBAN x = DAOUtil.layContext().DANHSACHCUNGCAPCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHSACHCUNGCAPCANBAN_DTO ob = new DTO.DANHSACHCUNGCAPCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHSACHCUNGCAPCANBAN_DTO ob = (DTO.DANHSACHCUNGCAPCANBAN_DTO)d;
            DANHSACHCUNGCAPCANBAN i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHSACHCUNGCAPCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DANHSACHCUNGCAPCANBAN x = DAOUtil.layContext().DANHSACHCUNGCAPCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHSACHCUNGCAPCANBAN_DTO ob = (DTO.DANHSACHCUNGCAPCANBAN_DTO)d;
            DANHSACHCUNGCAPCANBAN x = DAOUtil.layContext().DANHSACHCUNGCAPCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDCungCap = ob.IDCungCap;
            x.IDChuyenGiaoCanBan = ob.IDChuyenGiaoCanBan;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHSACHCHUYENGIAOCANBAN> x = DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.ToList<DANHSACHCHUYENGIAOCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHSACHCHUYENGIAOCANBAN_DTO xlq = new DTO.DANHSACHCHUYENGIAOCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class DANHSACHDOANHNGHIEPQUANTAM_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHSACHDOANHNGHIEPQUANTAM x = DAOUtil.layContext().DANHSACHDOANHNGHIEPQUANTAMs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO ob = new DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO ob = (DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO)d;
            DANHSACHDOANHNGHIEPQUANTAM i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHSACHDOANHNGHIEPQUANTAMs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DANHSACHDOANHNGHIEPQUANTAM x = DAOUtil.layContext().DANHSACHDOANHNGHIEPQUANTAMs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO ob = (DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO)d;
            DANHSACHDOANHNGHIEPQUANTAM x = DAOUtil.layContext().DANHSACHDOANHNGHIEPQUANTAMs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDDoanhNghiep = ob.IDDoanhNghiep;
            x.IDTaiKhoanQuanTam = ob.IDTaiKhoanQuanTam;
            x.NgayQuanTam = ob.NgayQuanTam;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHSACHDOANHNGHIEPQUANTAM> x = DAOUtil.layContext().DANHSACHDOANHNGHIEPQUANTAMs.ToList<DANHSACHDOANHNGHIEPQUANTAM>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO xlq = new DTO.DANHSACHDOANHNGHIEPQUANTAM_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class DANHSACHLINHVUCQUANTAM_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHSACHLINHVUCQUANTAM x = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHSACHLINHVUCQUANTAM_DTO ob = new DTO.DANHSACHLINHVUCQUANTAM_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHSACHLINHVUCQUANTAM_DTO ob = (DTO.DANHSACHLINHVUCQUANTAM_DTO)d;
            DANHSACHLINHVUCQUANTAM i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            
            DANHSACHLINHVUCQUANTAM x = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHSACHLINHVUCQUANTAM_DTO ob = (DTO.DANHSACHLINHVUCQUANTAM_DTO)d;
            DANHSACHLINHVUCQUANTAM x = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDDanhMuc = ob.IDDanhMuc;
            x.IDTaiKhoanQuanTam = ob.IDTaiKhoanQuanTam;
            x.NgayQuanTam = ob.NgayQuanTam;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHSACHLINHVUCQUANTAM> x = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.ToList<DANHSACHLINHVUCQUANTAM>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHSACHLINHVUCQUANTAM_DTO xlq = new DTO.DANHSACHLINHVUCQUANTAM_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class DANHSACHTIMMUAQUANTAM_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DANHSACHTIMMUAQUANTAM x = DAOUtil.layContext().DANHSACHTIMMUAQUANTAMs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHSACHTIMMUAQUANTAM_DTO ob = new DTO.DANHSACHTIMMUAQUANTAM_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHSACHTIMMUAQUANTAM_DTO ob = (DTO.DANHSACHTIMMUAQUANTAM_DTO)d;

            DANHSACHTIMMUAQUANTAM i = DAOUtil.layContext().DANHSACHTIMMUAQUANTAMs.FirstOrDefault(e=>e.IDThongTinTimMua == ob.IDThongTinTimMua && e.IDTaiKhoanQuanTam == ob.IDTaiKhoanQuanTam);
            if (i != null)
            {
                i.Xoa = false;
            }
            else
            {
                i = ob.toObjectDatabase();
                i.NgayQuanTam = DateTime.Now;
                i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDTaiKhoanQuanTam);
                i.THONGTINTIMMUA = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(e => e.ID == i.IDThongTinTimMua);
                DAOUtil.layContext().DANHSACHTIMMUAQUANTAMs.InsertOnSubmit(i);
            }
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {

            DANHSACHTIMMUAQUANTAM x = DAOUtil.layContext().DANHSACHTIMMUAQUANTAMs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHSACHTIMMUAQUANTAM_DTO ob = (DTO.DANHSACHTIMMUAQUANTAM_DTO)d;
            DANHSACHTIMMUAQUANTAM x = DAOUtil.layContext().DANHSACHTIMMUAQUANTAMs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDTaiKhoanQuanTam = ob.IDTaiKhoanQuanTam;
            x.IDThongTinTimMua = ob.IDThongTinTimMua;
            x.NgayQuanTam = ob.NgayQuanTam;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHSACHTIMMUAQUANTAM> x = DAOUtil.layContext().DANHSACHTIMMUAQUANTAMs.ToList<DANHSACHTIMMUAQUANTAM>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHSACHTIMMUAQUANTAM_DTO xlq = new DTO.DANHSACHTIMMUAQUANTAM_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class DICHVUCANBAN_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            DICHVUCANBAN x = DAOUtil.layContext().DICHVUCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.DICHVUCANBAN_DTO ob = new DTO.DICHVUCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DICHVUCANBAN_DTO ob = (DTO.DICHVUCANBAN_DTO)d;
            DICHVUCANBAN i = ob.toObjectDatabase();
            DAOUtil.layContext().DICHVUCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DICHVUCANBAN x = DAOUtil.layContext().DICHVUCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DICHVUCANBAN_DTO ob = (DTO.DICHVUCANBAN_DTO)d;
            DICHVUCANBAN x = DAOUtil.layContext().DICHVUCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.DoiTuongSuDung = ob.DoiTuongSuDung;
            x.Xoa = ob.Xoa;
            //x.IDCanBan = ob.IDCanBan;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DICHVUCANBAN> x = DAOUtil.layContext().DICHVUCANBANs.ToList<DICHVUCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DICHVUCANBAN_DTO xlq = new DTO.DICHVUCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class DOANHNGHIEP_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            var kq = DAOUtil.layContext().DOANHNGHIEPs.Select(e => new { DisplayText = e.TenCuaHang, Value = e.ID }).ToList();
            return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
            DOANHNGHIEP x = DAOUtil.layContext().DOANHNGHIEPs.FirstOrDefault(a => a.ID == ID);
            DTO.DOANHNGHIEP_DTO ob = new DTO.DOANHNGHIEP_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DOANHNGHIEP_DTO ob = (DTO.DOANHNGHIEP_DTO)d;
            DOANHNGHIEP i = ob.toObjectDatabase();
            DAOUtil.layContext().DOANHNGHIEPs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DOANHNGHIEP x = DAOUtil.layContext().DOANHNGHIEPs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DOANHNGHIEP_DTO ob = (DTO.DOANHNGHIEP_DTO)d;
            DOANHNGHIEP x = DAOUtil.layContext().DOANHNGHIEPs.FirstOrDefault(a => a.ID == ob.ID);
            x.CacSanPhamDaSanXuat = ob.CacSanPhamDaSanXuat;
            x.ChucNangCongTy = ob.ChucNangCongTy;
            x.DiaChi = ob.DiaChi;
            x.GioiThieu = ob.GioiThieu;
            x.HinhAnh = ob.HinhAnh;
            x.NgayThanhLap = ob.NgayThanhLap;
            x.NguoiDaiDien = ob.NguoiDaiDien;
            x.TenCuaHang = ob.TenCuaHang;
            x.TongSoNhanVien = ob.TongSoNhanVien;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DOANHNGHIEP> x = DAOUtil.layContext().DOANHNGHIEPs.ToList<DOANHNGHIEP>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DOANHNGHIEP_DTO xlq = new DTO.DOANHNGHIEP_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            IEnumerable<DOANHNGHIEP> query = DAOUtil.layContext().DOANHNGHIEPs.AsEnumerable();

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("ID DESC"))
            {
                query = query.OrderByDescending(p => p.ID);
            }
            else if (sorting.Equals("ID ASC"))
            {
                query = query.OrderBy(p => p.ID);
            }
            else if (sorting.Equals("NgayThanhLap ASC"))
            {
                query = query.OrderBy(p => p.NgayThanhLap);
            }
            else if (sorting.Equals("NgayThanhLap DESC"))
            {
                query = query.OrderByDescending(p => p.NgayThanhLap);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("NguoiDaiDien ASC"))
            {
                query = query.OrderBy(p => p.NguoiDaiDien);
            }
            else if (sorting.Equals("NguoiDaiDien DESC"))
            {
                query = query.OrderByDescending(p => p.NguoiDaiDien);
            }
            else if (sorting.Equals("TenCuaHang ASC"))
            {
                query = query.OrderBy(p => p.TenCuaHang);
            }
            else if (sorting.Equals("TenCuaHang DESC"))
            {
                query = query.OrderByDescending(p => p.TenCuaHang);
            }
            else if (sorting.Equals("TongSoNhanVien ASC"))
            {
                query = query.OrderBy(p => p.TongSoNhanVien);
            }
            else if (sorting.Equals("TongSoNhanVien DESC"))
            {
                query = query.OrderByDescending(p => p.TongSoNhanVien);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.ID);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (DOANHNGHIEP item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.DOANHNGHIEP_DTO rv = new DTO.DOANHNGHIEP_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (DOANHNGHIEP item in query.ToList())
                    {
                        DTO.DOANHNGHIEP_DTO rv = new DTO.DOANHNGHIEP_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq; 
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }


    public class LINHVUC_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            var kq = DAOUtil.layContext().LINHVUCs.Select(e => new { DisplayText = e.Ten, Value = e.ID }).ToList();
            return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
            LINHVUC x = DAOUtil.layContext().LINHVUCs.FirstOrDefault(a => a.ID == ID);
            DTO.LINHVUC_DTO ob = new DTO.LINHVUC_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.LINHVUC_DTO ob = (DTO.LINHVUC_DTO)d;
            LINHVUC i = ob.toObjectDatabase();
            DAOUtil.layContext().LINHVUCs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            LINHVUC x = DAOUtil.layContext().LINHVUCs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.LINHVUC_DTO ob = (DTO.LINHVUC_DTO)d;
            LINHVUC x = DAOUtil.layContext().LINHVUCs.FirstOrDefault(a => a.ID == ob.ID);
            x.Ten = ob.Ten;
            x.IDLinhVucCha = ob.IDLinhVucCha;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<LINHVUC> x = DAOUtil.layContext().LINHVUCs.ToList<LINHVUC>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.LINHVUC_DTO xlq = new DTO.LINHVUC_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().LINHVUCs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            //throw new NotImplementedException();
            IEnumerable<LINHVUC> query = DAOUtil.layContext().LINHVUCs.AsEnumerable();

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("Ten DESC"))
            {
                query = query.OrderByDescending(p => p.Ten);
            }
            else if (sorting.Equals("Ten ASC"))
            {
                query = query.OrderBy(p => p.Ten);
            }
            else if (sorting.Equals("IDLinhVucCha ASC"))
            {
                query = query.OrderBy(p => p.IDLinhVucCha);
            }
            else if (sorting.Equals("IDLinhVucCha DESC"))
            {
                query = query.OrderByDescending(p => p.IDLinhVucCha);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }

            else
            {
                //m?c d?nh theo th?i gian
                query = query.OrderByDescending(p => p.Ten);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (LINHVUC item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.LINHVUC_DTO rv = new DTO.LINHVUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (LINHVUC item in query.ToList())
                    {
                        DTO.LINHVUC_DTO rv = new DTO.LINHVUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq; 
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class LINHVUCCANBAN_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            LINHVUCCANBAN x = DAOUtil.layContext().LINHVUCCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.LINHVUCCANBAN_DTO ob = new DTO.LINHVUCCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.LINHVUCCANBAN_DTO ob = (DTO.LINHVUCCANBAN_DTO)d;
            LINHVUCCANBAN i = ob.toObjectDatabase();
            DAOUtil.layContext().LINHVUCCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            LINHVUCCANBAN x = DAOUtil.layContext().LINHVUCCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.LINHVUCCANBAN_DTO ob = (DTO.LINHVUCCANBAN_DTO)d;
            LINHVUCCANBAN x = DAOUtil.layContext().LINHVUCCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDCanBan = ob.IDCanBan;
            x.IDLinhVuc = ob.IDLinhVuc;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<LINHVUCCANBAN> x = DAOUtil.layContext().LINHVUCCANBANs.ToList<LINHVUCCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.LINHVUCCANBAN_DTO xlq = new DTO.LINHVUCCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class LOAITAIKHOAN_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            var kq = DAOUtil.layContext().LOAITAIKHOANs.Select(e => new { DisplayText = e.TenLoaiTaiKhoan, Value = e.ID }).ToList();
            return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
            LOAITAIKHOAN x = DAOUtil.layContext().LOAITAIKHOANs.FirstOrDefault(a => a.ID == ID);
            DTO.LOAITAIKHOAN_DTO ob = new DTO.LOAITAIKHOAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.LOAITAIKHOAN_DTO ob = (DTO.LOAITAIKHOAN_DTO)d;
            LOAITAIKHOAN i = ob.toObjectDatabase();
            DAOUtil.layContext().LOAITAIKHOANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            LOAITAIKHOAN x = DAOUtil.layContext().LOAITAIKHOANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.LOAITAIKHOAN_DTO ob = (DTO.LOAITAIKHOAN_DTO)d;
            LOAITAIKHOAN x = DAOUtil.layContext().LOAITAIKHOANs.FirstOrDefault(a => a.ID == ob.ID);
            x.TenLoaiTaiKhoan = ob.TenLoaiTaiKhoan;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<LOAITAIKHOAN> x = DAOUtil.layContext().LOAITAIKHOANs.Where(e=>e.ID > 2).ToList<LOAITAIKHOAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.LOAITAIKHOAN_DTO xlq = new DTO.LOAITAIKHOAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().LOAITAIKHOANs.Where(e => e.ID > 2).Count();
        }
        public int LaySoLuongPQ()
        {
            return DAOUtil.layContext().LOAITAIKHOANs.Count();
        }
        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachLoaiTaiKhoan_PQ(int jtStartIndex, int jtPageSize, string jtSorting)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<LOAITAIKHOAN> x = DAOUtil.layContext().LOAITAIKHOANs.ToList<LOAITAIKHOAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.LOAITAIKHOAN_DTO xlq = new DTO.LOAITAIKHOAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
    }

    public partial class LOAITHANHVIEN_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            var kq = DAOUtil.layContext().LOAITHANHVIENs.Select(e => new { DisplayText = e.TenLoaiThanhVien, Value = e.ID }).ToList();
            return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
            LOAITHANHVIEN x = DAOUtil.layContext().LOAITHANHVIENs.FirstOrDefault(a => a.ID == ID);
            DTO.LOAITHANHVIEN_DTO ob = new DTO.LOAITHANHVIEN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.LOAITHANHVIEN_DTO ob = (DTO.LOAITHANHVIEN_DTO)d;
            LOAITHANHVIEN i = ob.toObjectDatabase();
            DAOUtil.layContext().LOAITHANHVIENs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            LOAITHANHVIEN x = DAOUtil.layContext().LOAITHANHVIENs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.LOAITHANHVIEN_DTO ob = (DTO.LOAITHANHVIEN_DTO)d;
            LOAITHANHVIEN x = DAOUtil.layContext().LOAITHANHVIENs.FirstOrDefault(a => a.ID == ob.ID);
            x.TenLoaiThanhVien = ob.TenLoaiThanhVien;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<LOAITHANHVIEN> x = DAOUtil.layContext().LOAITHANHVIENs.ToList<LOAITHANHVIEN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.LOAITHANHVIEN_DTO xlq = new DTO.LOAITHANHVIEN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }
    public partial class MUCDOPHATTRIENTHIETBI_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            MUCDOPHATTRIENTHIETBI x = DAOUtil.layContext().MUCDOPHATTRIENTHIETBIs.FirstOrDefault(a => a.ID == ID);
            DTO.MUCDOPHATTRIENTHIETBI_DTO ob = new DTO.MUCDOPHATTRIENTHIETBI_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.MUCDOPHATTRIENTHIETBI_DTO ob = (DTO.MUCDOPHATTRIENTHIETBI_DTO)d;
            MUCDOPHATTRIENTHIETBI i = ob.toObjectDatabase();
            DAOUtil.layContext().MUCDOPHATTRIENTHIETBIs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            MUCDOPHATTRIENTHIETBI x = DAOUtil.layContext().MUCDOPHATTRIENTHIETBIs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.MUCDOPHATTRIENTHIETBI_DTO ob = (DTO.MUCDOPHATTRIENTHIETBI_DTO)d;
            MUCDOPHATTRIENTHIETBI x = DAOUtil.layContext().MUCDOPHATTRIENTHIETBIs.FirstOrDefault(a => a.ID == ob.ID);
            x.Ten = ob.Ten;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<MUCDOPHATTRIENTHIETBI> x = DAOUtil.layContext().MUCDOPHATTRIENTHIETBIs.ToList<MUCDOPHATTRIENTHIETBI>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.MUCDOPHATTRIENTHIETBI_DTO xlq = new DTO.MUCDOPHATTRIENTHIETBI_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCuaChaoBan(int IDChaoBan)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();

            THONGTINTHIETBICONGNGHE ttTBCN = DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.Where(e=>e.IDCanBan == IDChaoBan).FirstOrDefault();
            if (ttTBCN == null || ttTBCN.IDMucDoPhatTrien == null)
	        {
		         return kq;
	        }
            
            DTO.MUCDOPHATTRIENTHIETBI_DTO xlq = new DTO.MUCDOPHATTRIENTHIETBI_DTO();
            xlq.fromObjectDatabase(ttTBCN.MUCDOPHATTRIENTHIETBI);
            kq.Add(xlq);
            return kq;
        }
    }

    public partial class PHANMEMCANBAN_DAO : DAO
    {

        public DTO.DTO LayTuID(int ID)
        {
            PHANMEMCANBAN x = DAOUtil.layContext().PHANMEMCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.PHANMEMCANBAN_DTO ob = new DTO.PHANMEMCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.PHANMEMCANBAN_DTO ob = (DTO.PHANMEMCANBAN_DTO)d;
            PHANMEMCANBAN i = ob.toObjectDatabase();
            DAOUtil.layContext().PHANMEMCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            PHANMEMCANBAN x = DAOUtil.layContext().PHANMEMCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.PHANMEMCANBAN_DTO ob = (DTO.PHANMEMCANBAN_DTO)d;
            PHANMEMCANBAN x = DAOUtil.layContext().PHANMEMCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.CauTruc = ob.CauTruc;
            x.ChucNang = ob.ChucNang;
            x.MucDoPhatTrien = ob.MucDoPhatTrien;
            x.YeuCauPhanCung = ob.YeuCauPhanCung;
            //x.IDCanBan = ob.IDCanBan;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<PHANMEMCANBAN> x = DAOUtil.layContext().PHANMEMCANBANs.ToList<PHANMEMCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.PHANMEMCANBAN_DTO xlq = new DTO.PHANMEMCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class RAOVAT_DAO : DAO
    {

        public DTO.DTO LayTuID(int ID)
        {
            RAOVAT x = DAOUtil.layContext().RAOVATs.FirstOrDefault(a => a.ID == ID);
            DTO.RAOVAT_DTO ob = new DTO.RAOVAT_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.RAOVAT_DTO ob = (DTO.RAOVAT_DTO)d;
            RAOVAT i = ob.toObjectDatabase();
            i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDTaiKhoanRaoVat);
            DAOUtil.layContext().RAOVATs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {

            RAOVAT x = DAOUtil.layContext().RAOVATs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.RAOVAT_DTO ob = (DTO.RAOVAT_DTO)d;
            RAOVAT x = DAOUtil.layContext().RAOVATs.FirstOrDefault(a => a.ID == ob.ID);
            x.ChuDe = ob.ChuDe;
            //x.IDTaiKhoanRaoVat = ob.IDTaiKhoanRaoVat;
            x.NoiDung = ob.NoiDung;
            x.ThoiGianRao = ob.ThoiGianRao;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO >();
            List<RAOVAT> x = DAOUtil.layContext().RAOVATs.ToList<RAOVAT>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.RAOVAT_DTO xlq = new DTO.RAOVAT_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public DTO.DTO_Loc LayDanhSachSuDungLoc(string tieuDe , int nguoiRao , int startIndex, int count, string sorting, Boolean isLayXoa)
        {

            IEnumerable<RAOVAT> query = null;
            if (isLayXoa)
	        {
		         query = DAOUtil.layContext().RAOVATs;
	        }
            else
            {
                query = DAOUtil.layContext().RAOVATs.Where(e=>e.Xoa == false);
            }

            if (nguoiRao > 0)
            {
                query = query.Where(p => p.IDTaiKhoanRaoVat == nguoiRao);
            }

            if (!string.IsNullOrEmpty(tieuDe))
            {
                query = query.Where(p => p.ChuDe.IndexOf(tieuDe, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("ThoiGianRao DESC"))
            {
                query = query.OrderByDescending(p => p.ThoiGianRao);
            }
            else if (sorting.Equals("ThoiGianRao ASC"))
            {
                query = query.OrderBy(p => p.ThoiGianRao);
            }
            else if (sorting.Equals("ChuDe ASC"))
            {
                query = query.OrderBy(p => p.ChuDe);
            }
            else if (sorting.Equals("ChuDe DESC"))
            {
                query = query.OrderByDescending(p => p.ChuDe);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("IDTaiKhoanRaoVat ASC"))
            {
                query = query.OrderBy(p => p.TAIKHOAN.TenDangNhap);
            }
            else if (sorting.Equals("IDTaiKhoanRaoVat DESC"))
            {
                query = query.OrderByDescending(p => p.TAIKHOAN.TenDangNhap);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.ThoiGianRao);
            }

            DTO.DTO_Loc kqLoc = new DTO.DTO_Loc();
            kqLoc.SoLuong = query.Count();

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (RAOVAT item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.RAOVAT_DTO rv = new DTO.RAOVAT_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (RAOVAT item in query.ToList())
                    {
                        DTO.RAOVAT_DTO rv = new DTO.RAOVAT_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            

            kqLoc.dtoList = kq;

            return kqLoc;
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().RAOVATs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayRaoVatQuanTamCoThamSo(int idNguoiDung, int startIndex, int count, string sorting)
        {
            IEnumerable<RAOVAT> query = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == idNguoiDung).RAOVATDUOCQUANTAMs.Where(e => e.Xoa == false).Select(e => e.RAOVAT).AsEnumerable();
            return LayDanhSachCoThamSo(startIndex, count, sorting, query);
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {   
            return LayDanhSachCoThamSo(startIndex, count, sorting, false);
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, Boolean isLayXoa)
        {
            IEnumerable<RAOVAT> query;
            if (isLayXoa)
	        {
		         query = DAOUtil.layContext().RAOVATs.AsEnumerable();
	        }
            else{
                query = DAOUtil.layContext().RAOVATs.Where(e=>e.Xoa == false).AsEnumerable();
            }
            return LayDanhSachCoThamSo(startIndex, count, sorting,query);
        }


        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, IEnumerable<RAOVAT> query )
        {
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("ThoiGianRao DESC"))
            {
                query = query.OrderByDescending(p => p.ThoiGianRao);   
            }
            else if (sorting.Equals("ThoiGianRao ASC"))
            {
                query = query.OrderBy(p => p.ThoiGianRao);
            }
            else if (sorting.Equals("ChuDe ASC"))
            {
                query = query.OrderBy(p => p.ChuDe);
            }
            else if (sorting.Equals("ChuDe DESC"))
            {
                query = query.OrderByDescending(p => p.ChuDe);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if(sorting.Equals("IDTaiKhoanRaoVat ASC"))
            {
                query = query.OrderBy(p => p.TAIKHOAN.TenDangNhap);
            }
            else if (sorting.Equals("IDTaiKhoanRaoVat DESC"))
            {
                query = query.OrderByDescending(p => p.TAIKHOAN.TenDangNhap);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.ThoiGianRao);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (RAOVAT item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.RAOVAT_DTO rv =new DTO.RAOVAT_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (RAOVAT item in query.ToList())
                    {
                        DTO.RAOVAT_DTO rv = new DTO.RAOVAT_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq; 
        }

        public void Xoa(int id, int IDNguoiXoa)
        {
            RAOVAT x = DAOUtil.layContext().RAOVATs.FirstOrDefault(a => a.ID == id);
            if (IDNguoiXoa != x.IDTaiKhoanRaoVat)
            {
                throw new Exception("Không có quyền xoá rao vặt của tài khoản khác.");
            }
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }
    }
    public class RAOVATDUOCQUANTAM_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            RAOVATDUOCQUANTAM x = DAOUtil.layContext().RAOVATDUOCQUANTAMs.FirstOrDefault(a => a.ID == ID);
            DTO.RAOVATDUOCQUANTAM_DTO ob = new DTO.RAOVATDUOCQUANTAM_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.RAOVATDUOCQUANTAM_DTO ob = (DTO.RAOVATDUOCQUANTAM_DTO)d;
            RAOVATDUOCQUANTAM i = DAOUtil.layContext().RAOVATDUOCQUANTAMs.FirstOrDefault(e => e.IDRaoVat == ob.IDRaoVat && e.IDTaiKhoanQuanTam == ob.IDTaiKhoanQuanTam);

            if (i != null)
            {
                i.Xoa = false;
            }
            else
            {
                i = ob.toObjectDatabase();
                i.NgayQuanTam = DateTime.Now;
                i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDTaiKhoanQuanTam);
                i.RAOVAT = DAOUtil.layContext().RAOVATs.FirstOrDefault(e => e.ID == i.IDRaoVat);
                DAOUtil.layContext().RAOVATDUOCQUANTAMs.InsertOnSubmit(i);
            }
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            RAOVATDUOCQUANTAM x = DAOUtil.layContext().RAOVATDUOCQUANTAMs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.RAOVATDUOCQUANTAM_DTO ob = (DTO.RAOVATDUOCQUANTAM_DTO)d;
            RAOVATDUOCQUANTAM x = DAOUtil.layContext().RAOVATDUOCQUANTAMs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDRaoVat = ob.IDRaoVat;
            x.IDTaiKhoanQuanTam = ob.IDTaiKhoanQuanTam;
            x.NgayQuanTam = ob.NgayQuanTam;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<RAOVATDUOCQUANTAM> x = DAOUtil.layContext().RAOVATDUOCQUANTAMs.ToList<RAOVATDUOCQUANTAM>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.RAOVATDUOCQUANTAM_DTO xlq = new DTO.RAOVATDUOCQUANTAM_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class TAIKHOAN_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            var kq = DAOUtil.layContext().TAIKHOANs.Select(e => new { DisplayText = e.TenDangNhap, Value = e.ID }).ToList();
            return kq;
        }

        public DTO.TAIKHOAN_DTO LayTuTenDanhNhap_MatKhau(String tenDN, String MK)
        {
            TAIKHOAN x = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.TenDangNhap.Equals(tenDN) == true);

            if (x == null)
            {
                throw new Exception("Tên đăng nhập không tồn tại.");
            }

            if (HashMatKhau.VerifyMd5Hash(MK, x.MatKhau) == false)
            {
                throw new Exception("Mật khẩu không chính xác.");
            }

            DTO.TAIKHOAN_DTO ob = new DTO.TAIKHOAN_DTO();
            ob.ID = x.ID;
            ob.TenDangNhap = x.TenDangNhap;
            ob.IDLoaiTK = x.IDLoaiTK;
            ob.IDLoaiTV = x.IDLoaiTV;
            return ob;
        }

        public DTO.DTO LayTuID(int ID)
        {
            TAIKHOAN x = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.ID == ID);
            DTO.TAIKHOAN_DTO ob = new DTO.TAIKHOAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.TAIKHOAN_DTO ob = (DTO.TAIKHOAN_DTO)d;
            TAIKHOAN i = ob.toObjectDatabase();
            if ( i.IDDoanhNghiep != null)
            {
                i.DOANHNGHIEP = DAOUtil.layContext().DOANHNGHIEPs.FirstOrDefault(e => e.ID == i.IDDoanhNghiep);    
            }
            i.MatKhau = HashMatKhau.GetMd5Hash(ob.MatKhau);
            i.LOAITAIKHOAN = DAOUtil.layContext().LOAITAIKHOANs.FirstOrDefault(e => e.ID == i.IDLoaiTK);
            i.LOAITHANHVIEN= DAOUtil.layContext().LOAITHANHVIENs.FirstOrDefault(e => e.ID == i.IDLoaiTV);    

            DAOUtil.layContext().TAIKHOANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            TAIKHOAN x = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.TAIKHOAN_DTO ob = (DTO.TAIKHOAN_DTO)d;
            TAIKHOAN x = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.ID == ob.ID);
            x.DiaChi = ob.DiaChi;
            x.DiDong = ob.DiDong;
            x.DienThoai = ob.DienThoai;
            x.Email = ob.Email;
            x.Fax = ob.Fax;
            x.GioiTinh = ob.GioiTinh;
            x.HoTen = ob.HoTen;
            //x.IDDoanhNghiep = ob.IDDoanhNghiep;

            //x.IDLoaiTK = ob.IDLoaiTK;
            //x.IDLoaiTV = ob.IDLoaiTV;
            if (!x.MatKhau.Equals(ob.MatKhau))
            {
                x.MatKhau = HashMatKhau.GetMd5Hash(ob.MatKhau);
            }
            x.NgaySinh = ob.NgaySinh;
            //x.TenDangNhap = ob.TenDangNhap;
            x.Xoa = ob.Xoa;
            //i.DOANHNGHIEP = DAOUtil.layContext().DOANHNGHIEPs.FirstOrDefault(e => e.ID == i.IDDoanhNghiep);
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<TAIKHOAN> x = DAOUtil.layContext().TAIKHOANs.ToList<TAIKHOAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.TAIKHOAN_DTO xlq = new DTO.TAIKHOAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public DTO.DTO_Loc LayDanhSachSuDungLoc(String tenDangNhap, String hoTen, String Email, String DienThoai, int loaiThanhVien, int loaiTaiKhoan, int startIndex, int count, string sorting, bool isLayXoa)
        {
            IEnumerable<TAIKHOAN> query = null;

            if (isLayXoa)
            {
                query = DAOUtil.layContext().TAIKHOANs.AsEnumerable();
            }
            else
            {
                query = DAOUtil.layContext().TAIKHOANs.Where(e => e.Xoa == false).AsEnumerable();
            }
            if (loaiThanhVien > 0)
            {
                query = query.Where(p => p.IDLoaiTV == loaiThanhVien);
            }
            if (loaiTaiKhoan > 0)
            {
                query = query.Where(p => p.IDLoaiTK == loaiTaiKhoan);
            }

            if (!string.IsNullOrEmpty(tenDangNhap))
            {
                query = query.Where(p => p.TenDangNhap.IndexOf(tenDangNhap, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            if (!string.IsNullOrEmpty(hoTen))
            {
                query = query.Where(p => p.HoTen.IndexOf(hoTen, StringComparison.OrdinalIgnoreCase) >= 0);
            }
            if (!string.IsNullOrEmpty(Email))
            {
                query = query.Where(p => p.Email.IndexOf(Email, StringComparison.OrdinalIgnoreCase) >= 0);
            }
            if (!string.IsNullOrEmpty(DienThoai))
            {
                query = query.Where(p => p.DienThoai.IndexOf(DienThoai, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("TenDangNhap DESC"))
            {
                query = query.OrderByDescending(p => p.TenDangNhap);
            }
            else if (sorting.Equals("TenDangNhap ASC "))
            {
                query = query.OrderBy(p => p.TenDangNhap);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("IDDoanhNghiep ASC"))
            {
                query = query.OrderBy(p => p.IDDoanhNghiep);
            }
            else if (sorting.Equals("IDDoanhNghiep DESC"))
            {
                query = query.OrderByDescending(p => p.IDDoanhNghiep);
            }
            else if (sorting.Equals("IDLoaiTK ASC"))
            {
                query = query.OrderBy(p => p.IDLoaiTK);
            }
            else if (sorting.Equals("IDLoaiTK DESC"))
            {
                query = query.OrderByDescending(p => p.IDLoaiTK);
            }
            else if (sorting.Equals("IDLoaiTV ASC"))
            {
                query = query.OrderBy(p => p.IDLoaiTV);
            }
            else if (sorting.Equals("IDLoaiTV DESC"))
            {
                query = query.OrderByDescending(p => p.IDLoaiTV);
            }
            else if (sorting.Equals("DiaChi ASC"))
            {
                query = query.OrderBy(p => p.DiaChi);
            }
            else if (sorting.Equals("DiaChi DESC"))
            {
                query = query.OrderByDescending(p => p.DiaChi);
            }
            else if (sorting.Equals("DiDong ASC"))
            {
                query = query.OrderBy(p => p.DiDong);
            }
            else if (sorting.Equals("DiDong DESC"))
            {
                query = query.OrderByDescending(p => p.DiDong);
            }
            else if (sorting.Equals("DienThoai ASC"))
            {
                query = query.OrderBy(p => p.DienThoai);
            }
            else if (sorting.Equals("DienThoai DESC"))
            {
                query = query.OrderByDescending(p => p.DienThoai);
            }
            else if (sorting.Equals("Email ASC"))
            {
                query = query.OrderBy(p => p.Email);
            }
            else if (sorting.Equals("Email DESC"))
            {
                query = query.OrderByDescending(p => p.Email);
            }
            else if (sorting.Equals("NgaySinh ASC"))
            {
                query = query.OrderBy(p => p.NgaySinh);
            }
            else if (sorting.Equals("NgaySinh DESC"))
            {
                query = query.OrderByDescending(p => p.NgaySinh);
            }
            else if (sorting.Equals("HoTen ASC"))
            {
                query = query.OrderBy(p => p.HoTen);
            }
            else if (sorting.Equals("HoTen DESC"))
            {
                query = query.OrderByDescending(p => p.HoTen);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.ID);
            }

            DTO.DTO_Loc kqLoc = new DTO.DTO_Loc();
            kqLoc.SoLuong = query.Count();

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (TAIKHOAN item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.TAIKHOAN_DTO rv = new DTO.TAIKHOAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (TAIKHOAN item in query.ToList())
                    {
                        DTO.TAIKHOAN_DTO rv = new DTO.TAIKHOAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            kqLoc.dtoList = kq;

            return kqLoc;
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().TAIKHOANs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            IEnumerable<TAIKHOAN> query = DAOUtil.layContext().TAIKHOANs.AsEnumerable();

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("TenDangNhap DESC"))
            {
                query = query.OrderByDescending(p => p.TenDangNhap);
            }
            else if (sorting.Equals("TenDangNhap ASC "))
            {
                query = query.OrderBy(p => p.TenDangNhap);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("IDDoanhNghiep ASC"))
            {
                query = query.OrderBy(p => p.IDDoanhNghiep);
            }
            else if (sorting.Equals("IDDoanhNghiep DESC"))
            {
                query = query.OrderByDescending(p => p.IDDoanhNghiep);
            }
            else if (sorting.Equals("IDLoaiTK ASC"))
            {
                query = query.OrderBy(p => p.IDLoaiTK);
            }
            else if (sorting.Equals("IDLoaiTK DESC"))
            {
                query = query.OrderByDescending(p => p.IDLoaiTK);
            }
            else if (sorting.Equals("IDLoaiTV ASC"))
            {
                query = query.OrderBy(p => p.IDLoaiTV);
            }
            else if (sorting.Equals("IDLoaiTV DESC"))
            {
                query = query.OrderByDescending(p => p.IDLoaiTV);
            }
            else if (sorting.Equals("DiaChi ASC"))
            {
                query = query.OrderBy(p => p.DiaChi);
            }
            else if (sorting.Equals("DiaChi DESC"))
            {
                query = query.OrderByDescending(p => p.DiaChi);
            }
            else if (sorting.Equals("DiDong ASC"))
            {
                query = query.OrderBy(p => p.DiDong);
            }
            else if (sorting.Equals("DiDong DESC"))
            {
                query = query.OrderByDescending(p => p.DiDong);
            }
            else if (sorting.Equals("DienThoai ASC"))
            {
                query = query.OrderBy(p => p.DienThoai);
            }
            else if (sorting.Equals("DienThoai DESC"))
            {
                query = query.OrderByDescending(p => p.DienThoai);
            }
            else if (sorting.Equals("Email ASC"))
            {
                query = query.OrderBy(p => p.Email);
            }
            else if (sorting.Equals("Email DESC"))
            {
                query = query.OrderByDescending(p => p.Email);
            }
            else if (sorting.Equals("NgaySinh ASC"))
            {
                query = query.OrderBy(p => p.NgaySinh);
            }
            else if (sorting.Equals("NgaySinh DESC"))
            {
                query = query.OrderByDescending(p => p.NgaySinh);
            }
            else if (sorting.Equals("HoTen ASC"))
            {
                query = query.OrderBy(p => p.HoTen);
            }
            else if (sorting.Equals("HoTen DESC"))
            {
                query = query.OrderByDescending(p => p.HoTen);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.ID);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (TAIKHOAN item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.TAIKHOAN_DTO rv = new DTO.TAIKHOAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (TAIKHOAN item in query.ToList())
                    {
                        DTO.TAIKHOAN_DTO rv = new DTO.TAIKHOAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq;
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public DTO.TAIKHOAN_DTO Them(DTO.TAIKHOAN_DTO tk, List<int> ldanhMucQuanTam, DTO.DOANHNGHIEP_DTO dn, List<int> ldanhMucQuanTamDN)
        {
            DTO.TAIKHOAN_DTO ob = tk;
            TAIKHOAN i = ob.toObjectDatabase();          

            i.MatKhau = HashMatKhau.GetMd5Hash(ob.MatKhau);
            i.LOAITAIKHOAN = DAOUtil.layContext().LOAITAIKHOANs.FirstOrDefault(e => e.ID == i.IDLoaiTK);
            i.LOAITHANHVIEN = DAOUtil.layContext().LOAITHANHVIENs.FirstOrDefault(e => e.ID == i.IDLoaiTV);
            

            DAOUtil.layContext().TAIKHOANs.InsertOnSubmit(i);
            for (int j = 0; j < ldanhMucQuanTam.Count; j++)
            {
                DANHSACHLINHVUCQUANTAM dmTK = new DANHSACHLINHVUCQUANTAM();
                dmTK.TAIKHOAN = i;
                dmTK.DANHMUC = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == ldanhMucQuanTam[j]);
                dmTK.NgayQuanTam = DateTime.Now;
                DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.InsertOnSubmit(dmTK);
            }

            DOANHNGHIEP dni = dn.toObjectDatabase();
            //thên danh sách danh mục cho DN
            DAOUtil.layContext().DOANHNGHIEPs.InsertOnSubmit(dni);

            for (int j = 0; j < ldanhMucQuanTamDN.Count; j++)
            {
                DANHMUCDOANHNGHIEP dmDN = new DANHMUCDOANHNGHIEP();
                dmDN.DOANHNGHIEP = dni;
                dmDN.DANHMUC = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == ldanhMucQuanTamDN[j]);
                DAOUtil.layContext().DANHMUCDOANHNGHIEPs.InsertOnSubmit(dmDN);
            }

            i.DOANHNGHIEP = dni;

            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public DTO.TAIKHOAN_DTO Them(DTO.TAIKHOAN_DTO tk, List<int> ldanhMucQuanTam)
        {
            DTO.TAIKHOAN_DTO ob = tk;
            TAIKHOAN i = ob.toObjectDatabase();

            i.MatKhau = HashMatKhau.GetMd5Hash(ob.MatKhau);
            i.LOAITAIKHOAN = DAOUtil.layContext().LOAITAIKHOANs.FirstOrDefault(e => e.ID == i.IDLoaiTK);
            i.LOAITHANHVIEN = DAOUtil.layContext().LOAITHANHVIENs.FirstOrDefault(e => e.ID == i.IDLoaiTV);


            DAOUtil.layContext().TAIKHOANs.InsertOnSubmit(i);
            for (int j = 0; j < ldanhMucQuanTam.Count; j++)
            {
                DANHSACHLINHVUCQUANTAM dmTK = new DANHSACHLINHVUCQUANTAM();
                dmTK.TAIKHOAN = i;
                dmTK.DANHMUC = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == ldanhMucQuanTam[j]);
                dmTK.NgayQuanTam = DateTime.Now;
                DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.InsertOnSubmit(dmTK);
            }

            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void CapNhat(DTO.TAIKHOAN_DTO tk, List<int> ldanhMucQuanTam, DTO.DOANHNGHIEP_DTO dn, List<int> ldanhMucQuanTamDN)
        {
            DTO.TAIKHOAN_DTO ob = tk;
            TAIKHOAN x = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.ID == ob.ID);
            x.DiaChi = ob.DiaChi;
            x.DiDong = ob.DiDong;
            x.DienThoai = ob.DienThoai;
            x.Email = ob.Email;
            x.Fax = ob.Fax;
            x.GioiTinh = ob.GioiTinh;
            x.HoTen = ob.HoTen;
            x.NgaySinh = ob.NgaySinh;
            //x.TenDangNhap = ob.TenDangNhap;
            x.Xoa = ob.Xoa;

            List<DANHSACHLINHVUCQUANTAM> lDMQT= DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.Where(e => e.IDTaiKhoanQuanTam == x.ID).ToList();
            //xoá những danh mục đang quan tâm
            for (int i = 0; i < lDMQT.Count; i++)
            {
                lDMQT[i].Xoa = true;
            }

            //thêm những danh mục mới vào
            for (int j = 0; j < ldanhMucQuanTam.Count; j++)
            {
                int idDM = ldanhMucQuanTam[j];
                DANHSACHLINHVUCQUANTAM dmTK = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.Where(e => e.IDTaiKhoanQuanTam == x.ID && e.IDDanhMuc == idDM).FirstOrDefault();

                //tạo mới
                if (dmTK == null)
                {
                    dmTK = new DANHSACHLINHVUCQUANTAM();
                    dmTK.TAIKHOAN = x;
                    dmTK.DANHMUC = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == ldanhMucQuanTam[j]);
                    dmTK.NgayQuanTam = DateTime.Now;
                    DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.InsertOnSubmit(dmTK);
                }
                else
                {
                    //nếu có tồn tại thì sửa xoá
                    dmTK.Xoa = false;
                }
            }

            DOANHNGHIEP dni = x.DOANHNGHIEP;
            //cập nhật

            List<DANHMUCDOANHNGHIEP> lDMQTDN = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.Where(e => e.IDDoanhNghiep == dni.ID).ToList();
            //xoá những danh mục đang quan tâm
            for (int i = 0; i < lDMQTDN.Count; i++)
            {
                lDMQTDN[i].Xoa = true;
            }

            //thêm những danh mục mới vào
            for (int j = 0; j < ldanhMucQuanTamDN.Count; j++)
            {
                int idDM = ldanhMucQuanTamDN[j];
                DANHMUCDOANHNGHIEP dmDN = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.Where(e => e.IDDoanhNghiep == dn.ID && e.IDDanhMuc == idDM).FirstOrDefault();

                //tạo mới
                if (dmDN == null)
                {
                    dmDN = new DANHMUCDOANHNGHIEP();
                    dmDN.DOANHNGHIEP = dni;
                    dmDN.DANHMUC = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == ldanhMucQuanTamDN[j]);
                    DAOUtil.layContext().DANHMUCDOANHNGHIEPs.InsertOnSubmit(dmDN);
                }
                else
                {
                    //nếu có tồn tại thì sửa xoá
                    dmDN.Xoa = false;
                }
            }

            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.TAIKHOAN_DTO tk, List<int> ldanhMucQuanTam)
        {
            DTO.TAIKHOAN_DTO ob = tk;
            TAIKHOAN x = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.ID == ob.ID);
            x.DiaChi = ob.DiaChi;
            x.DiDong = ob.DiDong;
            x.DienThoai = ob.DienThoai;
            x.Email = ob.Email;
            x.Fax = ob.Fax;
            x.GioiTinh = ob.GioiTinh;
            x.HoTen = ob.HoTen;
            x.NgaySinh = ob.NgaySinh;
            //x.TenDangNhap = ob.TenDangNhap;
            x.Xoa = ob.Xoa;

            List<DANHSACHLINHVUCQUANTAM> lDMQT = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.Where(e => e.IDTaiKhoanQuanTam == x.ID).ToList();
            //xoá những danh mục đang quan tâm
            for (int i = 0; i < lDMQT.Count; i++)
            {
                lDMQT[i].Xoa = true;
            }

            //thêm những danh mục mới vào
            for (int j = 0; j < ldanhMucQuanTam.Count; j++)
            {
                int idDM = ldanhMucQuanTam[j];
                DANHSACHLINHVUCQUANTAM dmTK = DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.Where(e => e.IDTaiKhoanQuanTam == x.ID && e.IDDanhMuc == idDM).FirstOrDefault();

                //tạo mới
                if (dmTK == null)
                {
                    dmTK = new DANHSACHLINHVUCQUANTAM();
                    dmTK.TAIKHOAN = x;
                    dmTK.DANHMUC = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == ldanhMucQuanTam[j]);
                    dmTK.NgayQuanTam = DateTime.Now;
                    dmTK.Xoa = false;
                    DAOUtil.layContext().DANHSACHLINHVUCQUANTAMs.InsertOnSubmit(dmTK);
                }
                else
                {
                    //nếu có tồn tại thì sửa xoá
                    dmTK.Xoa = false;
                }
            }

            DAOUtil.layContext().SubmitChanges();
        }

        public void DoiMatKhau(int IDTK, string mkCu, string mkMoi)
        {
            TAIKHOAN x = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.ID == IDTK);
            if (x != null)
            {
                mkCu = HashMatKhau.GetMd5Hash(mkCu);
                if (mkCu.CompareTo(x.MatKhau) != 0)
                {
                    throw new Exception("Mật khẩu cũ chưa chính xác");
                }
                else
                {
                    x.MatKhau = HashMatKhau.GetMd5Hash(mkMoi);
                }
            }

        }

        public string LayTenNguoiNhanLienHeTuID(int id)
        {
            String ten = "";
            TAIKHOAN tk = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(a => a.ID == id);
            if (tk!=null)
            {
                if (tk.IDDoanhNghiep != null)
                {
                    ten += tk.DOANHNGHIEP.TenCuaHang +" <"+ tk.HoTen +">";
                }else
                {
                    ten += tk.HoTen;
                }
                
            }
            return ten;
        }
    }
    public partial class THONGDIEP_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            THONGDIEP x = DAOUtil.layContext().THONGDIEPs.FirstOrDefault(a => a.ID == ID);
            DTO.THONGDIEP_DTO ob = new DTO.THONGDIEP_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            
            DTO.THONGDIEP_DTO ob = (DTO.THONGDIEP_DTO)d;
            if (ob.NguoiGoi == ob.NguoihNan)
            {
                throw new Exception("Không thể tự gởi thông điệp đến bản thân.");
            }

            THONGDIEP i = ob.toObjectDatabase();
            i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.NguoiGoi);
            i.TAIKHOAN1 = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.NguoiNhan);
            i.ThoiGianGoi = DateTime.Now;
            DAOUtil.layContext().THONGDIEPs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            THONGDIEP x = DAOUtil.layContext().THONGDIEPs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.THONGDIEP_DTO ob = (DTO.THONGDIEP_DTO)d;
            THONGDIEP x = DAOUtil.layContext().THONGDIEPs.FirstOrDefault(a => a.ID == ob.ID);
            x.ChuDe = ob.ChuDe;
            x.EmailNguoiGoi = ob.EmailNguoiGoi;
            x.NguoiGoi = ob.NguoiGoi;
            x.NguoiNhan = ob.NguoihNan;
            x.NoiDung = ob.NoiDung;
            x.ThoiGianGoi = ob.ThoiGianGoi;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<THONGDIEP> x = DAOUtil.layContext().THONGDIEPs.ToList<THONGDIEP>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.THONGDIEP_DTO xlq = new DTO.THONGDIEP_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().THONGDIEPs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            IEnumerable<THONGDIEP> query = DAOUtil.layContext().THONGDIEPs.AsEnumerable();

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("ThoiGianGoi DESC"))
            {
                query = query.OrderByDescending(p => p.ThoiGianGoi);
            }
            else if (sorting.Equals("ThoiGianGoi ASC "))
            {
                query = query.OrderBy(p => p.ThoiGianGoi);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("NguoiGoi ASC"))
            {
                query = query.OrderBy(p => p.TAIKHOAN.TenDangNhap);
            }
            else if (sorting.Equals("NguoiGoi DESC"))
            {
                query = query.OrderByDescending(p => p.TAIKHOAN.TenDangNhap);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.ID);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGDIEP item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGDIEP_DTO rv = new DTO.THONGDIEP_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGDIEP item in query.ToList())
                    {
                        DTO.THONGDIEP_DTO rv = new DTO.THONGDIEP_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq;
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class THONGTINCANBAN_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            THONGTINCANBAN x = DAOUtil.layContext().THONGTINCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.THONGTINCANBAN_DTO ob = new DTO.THONGTINCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.THONGTINCANBAN_DTO ob = (DTO.THONGTINCANBAN_DTO)d;
            THONGTINCANBAN i = ob.toObjectDatabase();
            i.NgayDang = DateTime.Now;
            i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e=>e.ID == i.IDNguoiDang);
            DAOUtil.layContext().THONGTINCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }


        public DTO.DTO ThemThietBiCongNghe(DTO.THONGTINCANBAN_DTO ob, DTO.THONGTINTHIETBICONGNGHE_DTO tbcn, DTO.THONGTINLAPDAT_DTO ttld, DTO.THONGTINCHUYENGIAOCANBAN_DTO ttcg, List<int> listChuyenGiao, List<int> listCungCap, List<int> listDanhMuc, List<int> listLinhVuc)
        {
            THONGTINCANBAN i = ob.toObjectDatabase();
            i.NgayDang = DateTime.Now;
            i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDNguoiDang);

            THONGTINCHUYENGIAOCANBAN ttcgCANBAN = ttcg.toObjectDatabase();
            //thêm khóa ngoại
            foreach (int item in listChuyenGiao)
            {
                DANHSACHCHUYENGIAOCANBAN x = new DANHSACHCHUYENGIAOCANBAN();
                CHUYENGIAO xxx = DAOUtil.layContext().CHUYENGIAOs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.CHUYENGIAO = xxx;
                    x.THONGTINCHUYENGIAOCANBAN = ttcgCANBAN;
                    DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.InsertOnSubmit(x);
                }
            }

            foreach (int item in listCungCap)
            {
                DANHSACHCUNGCAPCANBAN x = new DANHSACHCUNGCAPCANBAN();
                CUNGCAP xxx = DAOUtil.layContext().CUNGCAPs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.CUNGCAP = xxx;
                    x.THONGTINCHUYENGIAOCANBAN = ttcgCANBAN;
                    DAOUtil.layContext().DANHSACHCUNGCAPCANBANs.InsertOnSubmit(x);
                }
            }

            i.THONGTINCHUYENGIAOCANBAN = ttcgCANBAN;
            DAOUtil.layContext().THONGTINCHUYENGIAOCANBANs.InsertOnSubmit(ttcgCANBAN);

            DAOUtil.layContext().THONGTINCANBANs.InsertOnSubmit(i);
            //thêm khóa ngoại
            foreach (int item in listDanhMuc)
            {
                DANHMUCANBAN x = new DANHMUCANBAN();
                DANHMUC xxx = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.DANHMUC = xxx;
                    x.THONGTINCANBAN = i;
                    DAOUtil.layContext().DANHMUCANBANs.InsertOnSubmit(x);
                }
            }

            foreach (int item in listLinhVuc)
            {
                LINHVUCCANBAN x = new LINHVUCCANBAN();
                LINHVUC xxx = DAOUtil.layContext().LINHVUCs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.LINHVUC = xxx;
                    x.THONGTINCANBAN = i;
                    DAOUtil.layContext().LINHVUCCANBANs.InsertOnSubmit(x);
                }
            }

            THONGTINLAPDAT ttldCANBAN = ttld.toObjectDatabase();

            DAOUtil.layContext().THONGTINLAPDATs.InsertOnSubmit(ttldCANBAN);

            THONGTINTHIETBICONGNGHE tbcnCANBAN = tbcn.toObjectDatabase();
            tbcnCANBAN.THONGTINCANBAN = i;
            tbcnCANBAN.MUCDOPHATTRIENTHIETBI = DAOUtil.layContext().MUCDOPHATTRIENTHIETBIs.FirstOrDefault(e => e.ID == tbcn.IDMucDoPhatTrien);
            tbcnCANBAN.THONGTINCANBAN = i;
            tbcnCANBAN.THONGTINLAPDAT = ttldCANBAN;
            tbcnCANBAN.TIEUCHUANTHIETBI = DAOUtil.layContext().TIEUCHUANTHIETBIs.FirstOrDefault(e => e.ID == tbcn.IDTieuChuan);
            tbcnCANBAN.XUATXUDETAI = DAOUtil.layContext().XUATXUDETAIs.FirstOrDefault(e => e.ID == tbcn.IDXuatXuDeTai);

            DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.InsertOnSubmit(tbcnCANBAN);

            DAOUtil.layContext().SubmitChanges();

            ob.fromObjectDatabase(i);
            return ob;
        }

        public DTO.DTO ThemDichVu(DTO.THONGTINCANBAN_DTO ob, DTO.DICHVUCANBAN_DTO dvcb, DTO.THONGTINCHUYENGIAOCANBAN_DTO ttcg, List<int> listChuyenGiao, List<int> listCungCap, List<int> listDanhMuc, List<int> listLinhVuc)
        {
            THONGTINCANBAN i = ob.toObjectDatabase();
            i.NgayDang = DateTime.Now;
            i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDNguoiDang);

            THONGTINCHUYENGIAOCANBAN ttcgCANBAN = ttcg.toObjectDatabase();
            //thêm khóa ngoại
            foreach (int item in listChuyenGiao)
            {
                DANHSACHCHUYENGIAOCANBAN x = new DANHSACHCHUYENGIAOCANBAN();
                CHUYENGIAO xxx = DAOUtil.layContext().CHUYENGIAOs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.CHUYENGIAO = xxx;
                    x.THONGTINCHUYENGIAOCANBAN = ttcgCANBAN;
                    DAOUtil.layContext().DANHSACHCHUYENGIAOCANBANs.InsertOnSubmit(x);
                }
            }

            foreach (int item in listCungCap)
            {
                DANHSACHCUNGCAPCANBAN x = new DANHSACHCUNGCAPCANBAN();
                CUNGCAP xxx = DAOUtil.layContext().CUNGCAPs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.CUNGCAP = xxx;
                    x.THONGTINCHUYENGIAOCANBAN = ttcgCANBAN;
                    DAOUtil.layContext().DANHSACHCUNGCAPCANBANs.InsertOnSubmit(x);
                }
            }

            i.THONGTINCHUYENGIAOCANBAN = ttcgCANBAN;
            DAOUtil.layContext().THONGTINCHUYENGIAOCANBANs.InsertOnSubmit(ttcgCANBAN);

            DAOUtil.layContext().THONGTINCANBANs.InsertOnSubmit(i);
            //thêm khóa ngoại
            foreach (int item in listDanhMuc)
            {
                DANHMUCANBAN x = new DANHMUCANBAN();
                DANHMUC xxx = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.DANHMUC = xxx;
                    x.THONGTINCANBAN = i;
                    DAOUtil.layContext().DANHMUCANBANs.InsertOnSubmit(x);
                }
            }

            foreach (int item in listLinhVuc)
            {
                LINHVUCCANBAN x = new LINHVUCCANBAN();
                LINHVUC xxx = DAOUtil.layContext().LINHVUCs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.LINHVUC = xxx;
                    x.THONGTINCANBAN = i;
                    DAOUtil.layContext().LINHVUCCANBANs.InsertOnSubmit(x);
                }
            }

            DICHVUCANBAN dvCANBAN = dvcb.toObjectDatabase();
            dvCANBAN.THONGTINCANBAN = i;
            DAOUtil.layContext().DICHVUCANBANs.InsertOnSubmit(dvCANBAN);


            DAOUtil.layContext().SubmitChanges();

            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            THONGTINCANBAN x = DAOUtil.layContext().THONGTINCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.THONGTINCANBAN_DTO ob = (DTO.THONGTINCANBAN_DTO)d;
            THONGTINCANBAN x = DAOUtil.layContext().THONGTINCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.HinhAnhDinhKem = ob.HinhAnhDinhKem;
            //x.IDChuyenGiao = ob.IDChuyenGiao;
            //x.IDDichVu = ob.IDDichVu;
            //x.IDNguoiDang = ob.IDNguoiDang;
            //x.IDPhanMem = ob.IDPhanMem;
            //x.IDThietBi = ob.IDThietBi;
            x.LoaiCanBan = ob.LoaiCanBan;
            x.MoTa = ob.MoTa;
            x.TenAnh = ob.TenAnh;
            x.TenViet = ob.TenViet;
            x.TepTinDinhKem = ob.TepTinDinhKem;
            x.TuKhoa = ob.TuKhoa;
            x.UuDiem = ob.UuDiem;
            x.XuatXu = ob.XuatXu;
            x.Xoa = ob.Xoa;
            //x.NgayDang = ob.NgayDang;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<THONGTINCANBAN> x = DAOUtil.layContext().THONGTINCANBANs.ToList<THONGTINCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.THONGTINCANBAN_DTO xlq = new DTO.THONGTINCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public DTO.DTO_Loc LayDanhSachSuDungLoc(String tenViet, String tenAnh, int loaiCanBan, int nguoiDang, int startIndex, int count, string sorting, bool isLayXoa)
        {
            
            IEnumerable<THONGTINCANBAN> query = null;

            if (isLayXoa)
            {
                query =  DAOUtil.layContext().THONGTINCANBANs.AsEnumerable();
            }else{
                query = DAOUtil.layContext().THONGTINCANBANs.Where(e=>e.Xoa == false).AsEnumerable();
            }
            if (nguoiDang > 0)
            {
                query = query.Where(p => p.IDNguoiDang == nguoiDang);
            }
            if (loaiCanBan > 0)
            {
                query = query.Where(p => p.LoaiCanBan == loaiCanBan);
            }
            
            if (!string.IsNullOrEmpty(tenViet))
            {
                query = query.Where(p => p.TenViet.IndexOf(tenViet, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            if (!string.IsNullOrEmpty(tenAnh))
            {
                query = query.Where(p => p.TenAnh.IndexOf(tenAnh, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("NgayDang DESC"))
            {
                query = query.OrderByDescending(p => p.NgayDang);
            }
            else if (sorting.Equals("NgayDang ASC"))
            {
                query = query.OrderBy(p => p.NgayDang);
            }
            if (sorting.Equals("ID DESC"))
            {
                query = query.OrderByDescending(p => p.ID);
            }
            else if (sorting.Equals("ID ASC"))
            {
                query = query.OrderBy(p => p.ID);
            }
            else if (sorting.Equals("LoaiCanBan ASC"))
            {
                query = query.OrderBy(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("LoaiCanBan DESC"))
            {
                query = query.OrderByDescending(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("IDTaiKhoanRaoVat ASC"))
            {
                query = query.OrderBy(p => p.TAIKHOAN.TenDangNhap);
            }
            else if (sorting.Equals("IDTaiKhoanRaoVat DESC"))
            {
                query = query.OrderByDescending(p => p.TAIKHOAN.TenDangNhap);
            }
            else if (sorting.Equals("TenAnh ASC"))
            {
                query = query.OrderBy(p => p.TenAnh);
            }
            else if (sorting.Equals("TenAnh DESC"))
            {
                query = query.OrderByDescending(p => p.TenAnh);
            }
            else if (sorting.Equals("TenViet ASC"))
            {
                query = query.OrderBy(p => p.TenViet);
            }
            else if (sorting.Equals("TenViet DESC"))
            {
                query = query.OrderByDescending(p => p.TenViet);
            }
            else if (sorting.Equals("XuatXu ASC"))
            {
                query = query.OrderBy(p => p.XuatXu);
            }
            else if (sorting.Equals("XuatXu DESC"))
            {
                query = query.OrderByDescending(p => p.XuatXu);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.NgayDang);
            }

            DTO.DTO_Loc kqLoc = new DTO.DTO_Loc();
            kqLoc.SoLuong = query.Count();

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGTINCANBAN item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGTINCANBAN_DTO rv = new DTO.THONGTINCANBAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGTINCANBAN item in query.ToList())
                    {
                        DTO.THONGTINCANBAN_DTO rv = new DTO.THONGTINCANBAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            kqLoc.dtoList = kq;

            return kqLoc;
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().THONGTINCANBANs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }


    public List<DTO.DTO> LayChaoBanQuanTamCoThamSo(int idNguoiDung, int startIndex, int count, string sorting)
        {
            IEnumerable<THONGTINCANBAN> query = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e=> e.ID == idNguoiDung).DANHSACHCANBANQUANTAMs.Where(e=>e.Xoa == false).Select(e=>e.THONGTINCANBAN).AsEnumerable();

            return LayDanhSachCoThamSo(startIndex, count, sorting, query);
        }

    public List<DTO.DTO> LayDanhSachCoThamSo(int idNguoiDung, int startIndex, int count, string sorting)
    {
        return LayDanhSachCoThamSo(startIndex, count, sorting, false);
    }

    public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
    {
        IEnumerable<THONGTINCANBAN> query =  DAOUtil.layContext().THONGTINCANBANs.AsEnumerable();
        return LayDanhSachCoThamSo(startIndex, count, sorting,query);
    }

    List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, IEnumerable<THONGTINCANBAN> query)
        {
            //Sorting
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("NgayDang DESC"))
            {
                query = query.OrderByDescending(p => p.NgayDang);
            }
            else if (sorting.Equals("NgayDang ASC"))
            {
                query = query.OrderBy(p => p.NgayDang);
            }
            if(sorting.Equals("ID DESC"))
            {
                query = query.OrderByDescending(p => p.ID);
            }
            else if (sorting.Equals("ID ASC"))
            {
                query = query.OrderBy(p => p.ID);
            }
            else if (sorting.Equals("LoaiCanBan ASC"))
            {
                query = query.OrderBy(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("LoaiCanBan DESC"))
            {
                query = query.OrderByDescending(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("IDTaiKhoanRaoVat ASC"))
            {
                query = query.OrderBy(p => p.TAIKHOAN.TenDangNhap);
            }
            else if (sorting.Equals("IDTaiKhoanRaoVat DESC"))
            {
                query = query.OrderByDescending(p => p.TAIKHOAN.TenDangNhap);
            }
            else if (sorting.Equals("TenAnh ASC"))
            {
                query = query.OrderBy(p => p.TenAnh);
            }
            else if (sorting.Equals("TenAnh DESC"))
            {
                query = query.OrderByDescending(p => p.TenAnh);
            }
            else if (sorting.Equals("TenViet ASC"))
            {
                query = query.OrderBy(p => p.TenViet);
            }
            else if (sorting.Equals("TenViet DESC"))
            {
                query = query.OrderByDescending(p => p.TenViet);
            }
            else if (sorting.Equals("XuatXu ASC"))
            {
                query = query.OrderBy(p => p.XuatXu);
            }
            else if (sorting.Equals("XuatXu DESC"))
            {
                query = query.OrderByDescending(p => p.XuatXu);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.NgayDang);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGTINCANBAN item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGTINCANBAN_DTO rv = new DTO.THONGTINCANBAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGTINCANBAN item in query.ToList())
                    {
                        DTO.THONGTINCANBAN_DTO rv = new DTO.THONGTINCANBAN_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq; 
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isLayXoa)
        {

           IEnumerable<THONGTINCANBAN> query = null;
            if (isLayXoa)
	        {
		         query =  DAOUtil.layContext().THONGTINCANBANs.AsEnumerable();
	        }else{
                query =  DAOUtil.layContext().THONGTINCANBANs.Where(e=>e.Xoa == false).AsEnumerable();
            }

            return LayDanhSachCoThamSo(startIndex, count, sorting,query);
        }




        public void Xoa(int id, int IDNguoiXoa)
        {
            THONGTINCANBAN x = DAOUtil.layContext().THONGTINCANBANs.FirstOrDefault(a => a.ID == id);
            if (IDNguoiXoa != x.IDNguoiDang)
            {
                throw new Exception("Không thể xoá cần bán của tài khoản khác.");
            }
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }
    }
    public partial class THONGTINCHUYENGIAOCANBAN_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            THONGTINCHUYENGIAOCANBAN x = DAOUtil.layContext().THONGTINCHUYENGIAOCANBANs.FirstOrDefault(a => a.ID == ID);
            DTO.THONGTINCHUYENGIAOCANBAN_DTO ob = new DTO.THONGTINCHUYENGIAOCANBAN_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.THONGTINCHUYENGIAOCANBAN_DTO ob = (DTO.THONGTINCHUYENGIAOCANBAN_DTO)d;
            THONGTINCHUYENGIAOCANBAN i = ob.toObjectDatabase();
            DAOUtil.layContext().THONGTINCHUYENGIAOCANBANs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            THONGTINCHUYENGIAOCANBAN x = DAOUtil.layContext().THONGTINCHUYENGIAOCANBANs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.THONGTINCHUYENGIAOCANBAN_DTO ob = (DTO.THONGTINCHUYENGIAOCANBAN_DTO)d;
            THONGTINCHUYENGIAOCANBAN x = DAOUtil.layContext().THONGTINCHUYENGIAOCANBANs.FirstOrDefault(a => a.ID == ob.ID);
            x.BaoHanh = ob.BaoHanh;
            x.ChiPhiKhac = ob.ChiPhiKhac;
            x.DanhSachDonViChuyenGiao = ob.DanhSachDonViChuyenGiao;
            x.GiaKhoiDiem = ob.GiaKhoiDiem;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<THONGTINCHUYENGIAOCANBAN> x = DAOUtil.layContext().THONGTINCHUYENGIAOCANBANs.ToList<THONGTINCHUYENGIAOCANBAN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.THONGTINCHUYENGIAOCANBAN_DTO xlq = new DTO.THONGTINCHUYENGIAOCANBAN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }
    public partial class THONGTINLAPDAT_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            THONGTINLAPDAT x = DAOUtil.layContext().THONGTINLAPDATs.FirstOrDefault(a => a.ID == ID);
            DTO.THONGTINLAPDAT_DTO ob = new DTO.THONGTINLAPDAT_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }


        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.THONGTINLAPDAT_DTO ob = (DTO.THONGTINLAPDAT_DTO)d;
            THONGTINLAPDAT i = ob.toObjectDatabase();
            DAOUtil.layContext().THONGTINLAPDATs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            THONGTINLAPDAT x = DAOUtil.layContext().THONGTINLAPDATs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.THONGTINLAPDAT_DTO ob = (DTO.THONGTINLAPDAT_DTO)d;
            THONGTINLAPDAT x = DAOUtil.layContext().THONGTINLAPDATs.FirstOrDefault(a => a.ID == ob.ID);
            x.LuongDienTieuThu = ob.LuongDienTieuThu;
            x.LuongKhiDotDauTieuThu = ob.LuongKhiDotDauTieuThu;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<THONGTINLAPDAT> x = DAOUtil.layContext().THONGTINLAPDATs.ToList<THONGTINLAPDAT>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.THONGTINLAPDAT_DTO xlq = new DTO.THONGTINLAPDAT_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }
    public partial class THONGTINTHIETBICONGNGHE_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            THONGTINTHIETBICONGNGHE x = DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.FirstOrDefault(a => a.ID == ID);
            DTO.THONGTINTHIETBICONGNGHE_DTO ob = new DTO.THONGTINTHIETBICONGNGHE_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }


        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.THONGTINTHIETBICONGNGHE_DTO ob = (DTO.THONGTINTHIETBICONGNGHE_DTO)d;
            THONGTINTHIETBICONGNGHE i = ob.toObjectDatabase();
            DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            THONGTINTHIETBICONGNGHE x = DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.THONGTINTHIETBICONGNGHE_DTO ob = (DTO.THONGTINTHIETBICONGNGHE_DTO)d;
            THONGTINTHIETBICONGNGHE x = DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.FirstOrDefault(a => a.ID == ob.ID);
            x.ChiTieuKinhTeKyThuat = ob.ChiTieuKinhTeKyThuat;
            x.CongSuatNangSuat = ob.CongSuatNangSuat;
            x.IDMucDoPhatTrien = ob.IDMucDoPhatTrien;
            x.IDThongTinLapDat = ob.IDThongTinLapDat;
            x.IDTieuChuan = ob.IDTieuChuan;
            x.IDXuatXuDeTai = ob.IDXuatXuDeTai;
            x.NamHoanThien = ob.NamHoanThien;
            //x.IDCanBan = ob.IDCanBan;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<THONGTINTHIETBICONGNGHE> x = DAOUtil.layContext().THONGTINTHIETBICONGNGHEs.ToList<THONGTINTHIETBICONGNGHE>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.THONGTINTHIETBICONGNGHE_DTO xlq = new DTO.THONGTINTHIETBICONGNGHE_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class THONGTINTIMMUA_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            THONGTINTIMMUA x = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(a => a.ID == ID);
            DTO.THONGTINTIMMUA_DTO ob = new DTO.THONGTINTIMMUA_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }


        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.THONGTINTIMMUA_DTO ob = (DTO.THONGTINTIMMUA_DTO)d;
            THONGTINTIMMUA i = ob.toObjectDatabase();
            i.NgayDang = DateTime.Now;
            i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDNguoiDang);

            DAOUtil.layContext().THONGTINTIMMUAs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public DTO.DTO Them(DTO.DTO thongtin, List<int> listChuyenGiao, List<int> listDanhMuc, List<int> listTieuChuan)
        {
            DTO.THONGTINTIMMUA_DTO ob = (DTO.THONGTINTIMMUA_DTO)thongtin;
            THONGTINTIMMUA i = ob.toObjectDatabase();
            i.NgayDang = DateTime.Now;
            i.TAIKHOAN = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == i.IDNguoiDang);
            //thêm 
            DAOUtil.layContext().THONGTINTIMMUAs.InsertOnSubmit(i);

            //thêm khóa ngoại
            foreach (int item in listChuyenGiao)
            {
                CHUYENGIAOTIMMUA x = new CHUYENGIAOTIMMUA();
                CHUYENGIAO xxx= DAOUtil.layContext().CHUYENGIAOs.FirstOrDefault(e=>e.ID == item);
                if (xxx!= null)
	            {
                    x.CHUYENGIAO = xxx;
                    x.THONGTINTIMMUA = i;
                    DAOUtil.layContext().CHUYENGIAOTIMMUAs.InsertOnSubmit(x);
	            }
                
            }

            foreach (int item in listDanhMuc)
            {
                DANHMUCCANMUA x = new DANHMUCCANMUA();
                DANHMUC xxx = DAOUtil.layContext().DANHMUCs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.DANHMUC = xxx;
                    x.THONGTINTIMMUA = i;
                    DAOUtil.layContext().DANHMUCCANMUAs.InsertOnSubmit(x);
                }
            }

            foreach (int item in listTieuChuan)
            {
                TIEUCHUANTIMMUA x = new TIEUCHUANTIMMUA();
                TIEUCHUANTHIETBI xxx = DAOUtil.layContext().TIEUCHUANTHIETBIs.FirstOrDefault(e => e.ID == item);
                if (xxx != null)
                {
                    x.TIEUCHUANTHIETBI = xxx;
                    x.THONGTINTIMMUA = i;
                    DAOUtil.layContext().TIEUCHUANTIMMUAs.InsertOnSubmit(x);
                }
            }

            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }
        public void Xoa(int id)
        {
            THONGTINTIMMUA x = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.THONGTINTIMMUA_DTO ob = (DTO.THONGTINTIMMUA_DTO)d;
            THONGTINTIMMUA x = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(a => a.ID == ob.ID);
            //x.IDNguoiDang = ob.IDNguoiDang;
            x.LoaiCanBan = ob.LoaiCanBan;
            x.MoTa = ob.MoTa;
            x.SanXuatSauNam = ob.SanXuatSauNam;
            x.TenAnh = ob.TenAnh;
            x.TenViet = ob.TenViet;
            x.ThongTinKhac = ob.ThongTinKhac;
            x.TuKhoa = ob.TuKhoa;
            x.XuatXu = ob.XuatXu;
            x.Xoa = ob.Xoa;
            //x.NgayDang = ob.NgayDang;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<THONGTINTIMMUA> x = DAOUtil.layContext().THONGTINTIMMUAs.ToList<THONGTINTIMMUA>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.THONGTINTIMMUA_DTO xlq = new DTO.THONGTINTIMMUA_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSach(int max)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<THONGTINTIMMUA> x = DAOUtil.layContext().THONGTINTIMMUAs.ToList<THONGTINTIMMUA>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.THONGTINTIMMUA_DTO xlq = new DTO.THONGTINTIMMUA_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public DTO.DTO_Loc LayDanhSachSuDungLoc(String tenViet, String tenAnh, int loaiCanBan, int nguoiDang, int startIndex, int count, string sorting, bool isLayXoa)
        {
            IEnumerable<THONGTINTIMMUA> query = null;

            if (isLayXoa)
            {
                query = DAOUtil.layContext().THONGTINTIMMUAs.AsEnumerable();
            }
            else
            {
                query = DAOUtil.layContext().THONGTINTIMMUAs.Where(e => e.Xoa == false).AsEnumerable();
            }

            if (nguoiDang > 0)
            {
                query = query.Where(p => p.IDNguoiDang == nguoiDang);
            }
            if (loaiCanBan > 0)
            {
                query = query.Where(p => p.LoaiCanBan == loaiCanBan);
            }

            if (!string.IsNullOrEmpty(tenViet))
            {
                query = query.Where(p => p.TenViet.IndexOf(tenViet, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            if (!string.IsNullOrEmpty(tenAnh))
            {
                query = query.Where(p => p.TenAnh.IndexOf(tenAnh, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("NgayDang DESC"))
            {
                query = query.OrderByDescending(p => p.NgayDang);
            }
            else if (sorting.Equals("NgayDang ASC"))
            {
                query = query.OrderBy(p => p.NgayDang);
            }
            if (sorting.Equals("IDNguoiDang DESC"))
            {
                query = query.OrderByDescending(p => p.IDNguoiDang);
            }
            else if (sorting.Equals("IDNguoiDang ASC"))
            {
                query = query.OrderBy(p => p.IDNguoiDang);
            }
            else if (sorting.Equals("LoaiCanBan ASC"))
            {
                query = query.OrderBy(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("LoaiCanBan DESC"))
            {
                query = query.OrderByDescending(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("MoTa ASC"))
            {
                query = query.OrderBy(p => p.MoTa);
            }
            else if (sorting.Equals("MoTa DESC"))
            {
                query = query.OrderByDescending(p => p.MoTa);
            }

            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("TenViet ASC"))
            {
                query = query.OrderBy(p => p.TenViet);
            }
            else if (sorting.Equals("TenViet DESC"))
            {
                query = query.OrderByDescending(p => p.TenViet);
            }

            else if (sorting.Equals("TenAnh ASC"))
            {
                query = query.OrderBy(p => p.TenAnh);
            }
            else if (sorting.Equals("TenAnh DESC"))
            {
                query = query.OrderByDescending(p => p.TenAnh);
            }

            else
            {
                //m?c d?nh theo th?i gian
                query = query.OrderByDescending(p => p.NgayDang);
            }

            DTO.DTO_Loc kqLoc = new DTO.DTO_Loc();
            kqLoc.SoLuong = query.Count();

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGTINTIMMUA item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGTINTIMMUA item in query.ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            kqLoc.dtoList = kq;

            return kqLoc;
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().THONGTINTIMMUAs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayTimMuaQuanTamCoThamSo(int idNguoiDung, int startIndex, int count, string sorting)
        {
            IEnumerable<THONGTINTIMMUA> query = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == idNguoiDung).DANHSACHTIMMUAQUANTAMs.Where(e=>e.Xoa == false).Select(e=>e.THONGTINTIMMUA).AsEnumerable();
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("NgayDang DESC"))
            {
                query = query.OrderByDescending(p => p.NgayDang);
            }
            else if (sorting.Equals("NgayDang ASC"))
            {
                query = query.OrderBy(p => p.NgayDang);
            }
            if (sorting.Equals("IDNguoiDang DESC"))
            {
                query = query.OrderByDescending(p => p.IDNguoiDang);
            }
            else if (sorting.Equals("IDNguoiDang ASC"))
            {
                query = query.OrderBy(p => p.IDNguoiDang);
            }
            else if (sorting.Equals("LoaiCanBan ASC"))
            {
                query = query.OrderBy(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("LoaiCanBan DESC"))
            {
                query = query.OrderByDescending(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("MoTa ASC"))
            {
                query = query.OrderBy(p => p.MoTa);
            }
            else if (sorting.Equals("MoTa DESC"))
            {
                query = query.OrderByDescending(p => p.MoTa);
            }

            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("TenViet ASC"))
            {
                query = query.OrderBy(p => p.TenViet);
            }
            else if (sorting.Equals("TenViet DESC"))
            {
                query = query.OrderByDescending(p => p.TenViet);
            }

            else if (sorting.Equals("TenAnh ASC"))
            {
                query = query.OrderBy(p => p.TenAnh);
            }
            else if (sorting.Equals("TenAnh DESC"))
            {
                query = query.OrderByDescending(p => p.TenAnh);
            }

            else
            {
                //m?c d?nh theo th?i gian
                query = query.OrderByDescending(p => p.NgayDang);
            }
            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGTINTIMMUA item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGTINTIMMUA item in query.ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }
            return kq; 
        }

         public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            return LayDanhSachCoThamSo(startIndex,count,sorting,false);
        }

         public List<DTO.DTO> LayDanhSachCoThamSo(int idNguoiDung,int startIndex, int count, string sorting)
        {
            IEnumerable<THONGTINTIMMUA> query = DAOUtil.layContext().THONGTINTIMMUAs.Where(e=> e.IDNguoiDang == idNguoiDung).AsEnumerable();
            return LayDanhSachCoThamSo(startIndex, count, sorting,query);
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting,IEnumerable<THONGTINTIMMUA> query )
        {
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("NgayDang DESC"))
            {
                query = query.OrderByDescending(p => p.NgayDang);
            }
            else if (sorting.Equals("NgayDang ASC"))
            {
                query = query.OrderBy(p => p.NgayDang);
            }
            if (sorting.Equals("IDNguoiDang DESC"))
            {
                query = query.OrderByDescending(p => p.IDNguoiDang);
            }
            else if (sorting.Equals("IDNguoiDang ASC"))
            {
                query = query.OrderBy(p => p.IDNguoiDang);
            }
            else if (sorting.Equals("LoaiCanBan ASC"))
            {
                query = query.OrderBy(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("LoaiCanBan DESC"))
            {
                query = query.OrderByDescending(p => p.LoaiCanBan);
            }
            else if (sorting.Equals("MoTa ASC"))
            {
                query = query.OrderBy(p => p.MoTa);
            }
            else if (sorting.Equals("MoTa DESC"))
            {
                query = query.OrderByDescending(p => p.MoTa);
            }

            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("TenViet ASC"))
            {
                query = query.OrderBy(p => p.TenViet);
            }
            else if (sorting.Equals("TenViet DESC"))
            {
                query = query.OrderByDescending(p => p.TenViet);
            }

            else if (sorting.Equals("TenAnh ASC"))
            {
                query = query.OrderBy(p => p.TenAnh);
            }
            else if (sorting.Equals("TenAnh DESC"))
            {
                query = query.OrderByDescending(p => p.TenAnh);
            }

            else
            {
                //m?c d?nh theo th?i gian
                query = query.OrderByDescending(p => p.NgayDang);
            }
            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (THONGTINTIMMUA item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (THONGTINTIMMUA item in query.ToList())
                    {
                        DTO.THONGTINTIMMUA_DTO rv = new DTO.THONGTINTIMMUA_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }
            return kq; 
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isLayXoa)
        {
            IEnumerable<THONGTINTIMMUA> query = null;
            if (isLayXoa)
	        {
		        query = DAOUtil.layContext().THONGTINTIMMUAs.AsEnumerable();
	        }
            else
            {
                query = DAOUtil.layContext().THONGTINTIMMUAs.Where(e=> e.Xoa == false).AsEnumerable();
            }
            
            return LayDanhSachCoThamSo(startIndex, count, sorting,query);
        }

        public List<DTO.DTO> LayDanhSachDanhMuc(int id)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUC> x = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(e=>e.ID == id).DANHMUCCANMUAs.Select(e=>e.DANHMUC).ToList<DANHMUC>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUC_DTO xlq = new DTO.DANHMUC_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }


        public void Xoa(int id, int IDNguoiXoa)
        {
            THONGTINTIMMUA x = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(a => a.ID == id);
            if (IDNguoiXoa != x.IDNguoiDang)
            {
                throw new Exception("Không thể xoá tìm mua của tài khoản khác.");
            }
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }
    }

    public partial class TIEUCHUANTHIETBI_DAO : DAO
    {

        public DTO.DTO LayTuID(int ID)
        {
            TIEUCHUANTHIETBI x = DAOUtil.layContext().TIEUCHUANTHIETBIs.FirstOrDefault(a => a.ID == ID);
            DTO.TIEUCHUANTHIETBI_DTO ob = new DTO.TIEUCHUANTHIETBI_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.TIEUCHUANTHIETBI_DTO ob = (DTO.TIEUCHUANTHIETBI_DTO)d;
            TIEUCHUANTHIETBI i = ob.toObjectDatabase();
            DAOUtil.layContext().TIEUCHUANTHIETBIs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            TIEUCHUANTHIETBI x = DAOUtil.layContext().TIEUCHUANTHIETBIs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.TIEUCHUANTHIETBI_DTO ob = (DTO.TIEUCHUANTHIETBI_DTO)d;
            TIEUCHUANTHIETBI x = DAOUtil.layContext().TIEUCHUANTHIETBIs.FirstOrDefault(a => a.ID == ob.ID);
            x.Ten = ob.Ten;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<TIEUCHUANTHIETBI> x = DAOUtil.layContext().TIEUCHUANTHIETBIs.ToList<TIEUCHUANTHIETBI>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.TIEUCHUANTHIETBI_DTO xlq = new DTO.TIEUCHUANTHIETBI_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }
        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public partial class TINTUC_DAO : DAO
    {
        public DTO.DTO LayTuID(int ID)
        {
            TINTUC x = DAOUtil.layContext().TINTUCs.FirstOrDefault(a => a.ID == ID);
            DTO.TINTUC_DTO ob = new DTO.TINTUC_DTO();
            ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.TINTUC_DTO ob = (DTO.TINTUC_DTO)d;
            TINTUC i = ob.toObjectDatabase();
            i.NgayDang = DateTime.Now;
            DAOUtil.layContext().TINTUCs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            TINTUC x = DAOUtil.layContext().TINTUCs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;
            DAOUtil.layContext().SubmitChanges();
        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.TINTUC_DTO ob = (DTO.TINTUC_DTO)d;
            TINTUC x = DAOUtil.layContext().TINTUCs.FirstOrDefault(a => a.ID == ob.ID);
            //x.NgayDang = ob.NgayDang;
            x.NoiDung = ob.NoiDung;
            x.TieuDe = ob.TieuDe;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<TINTUC> x = DAOUtil.layContext().TINTUCs.ToList<TINTUC>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.TINTUC_DTO xlq = new DTO.TINTUC_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public DTO.DTO_Loc LayDanhSachSuDungLoc(string tieuDe, String noiDung, int startIndex, int count, string sorting)
        {
            IEnumerable<TINTUC> query = DAOUtil.layContext().TINTUCs.AsEnumerable();
            if (!string.IsNullOrEmpty(tieuDe))
            {
                query = query.Where(p => p.TieuDe.IndexOf(tieuDe, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            if (!string.IsNullOrEmpty(noiDung))
            {
                query = query.Where(p => p.NoiDung.IndexOf(noiDung, StringComparison.OrdinalIgnoreCase) >= 0);
            }

            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("NgayDang DESC"))
            {
                query = query.OrderByDescending(p => p.NgayDang);
            }
            else if (sorting.Equals("NgayDang ASC"))
            {
                query = query.OrderBy(p => p.NgayDang);
            }
            else if (sorting.Equals("NoiDung ASC"))
            {
                query = query.OrderBy(p => p.NoiDung);
            }
            else if (sorting.Equals("NoiDung DESC"))
            {
                query = query.OrderByDescending(p => p.NoiDung);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("TieuDe ASC"))
            {
                query = query.OrderBy(p => p.TieuDe);
            }
            else if (sorting.Equals("TieuDe DESC"))
            {
                query = query.OrderByDescending(p => p.TieuDe);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.NgayDang);
            }


            DTO.DTO_Loc kqLoc = new DTO.DTO_Loc();
            kqLoc.SoLuong = query.Count();

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (TINTUC item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.TINTUC_DTO rv = new DTO.TINTUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (TINTUC item in query.ToList())
                    {
                        DTO.TINTUC_DTO rv = new DTO.TINTUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            kqLoc.dtoList = kq;

            return kqLoc;
        }

        public int LaySoLuong()
        {
            return DAOUtil.layContext().TINTUCs.Count();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            return LayDanhSachCoThamSo( startIndex, count, sorting, false);
        }

        public List<DTO.DTO> LayDanhSachCoThamSo( int startIndex, int count, string sorting, IEnumerable<TINTUC> query)
        {
            //Sorting
            if (string.IsNullOrEmpty(sorting) || sorting.Equals("NgayDang DESC"))
            {
                query = query.OrderByDescending(p => p.NgayDang);
            }
            else if (sorting.Equals("NgayDang ASC"))
            {
                query = query.OrderBy(p => p.NgayDang);
            }
            else if (sorting.Equals("NoiDung ASC"))
            {
                query = query.OrderBy(p => p.NoiDung);
            }
            else if (sorting.Equals("NoiDung DESC"))
            {
                query = query.OrderByDescending(p => p.NoiDung);
            }
            else if (sorting.Equals("Xoa ASC"))
            {
                query = query.OrderBy(p => p.Xoa);
            }
            else if (sorting.Equals("Xoa DESC"))
            {
                query = query.OrderByDescending(p => p.Xoa);
            }
            else if (sorting.Equals("TieuDe ASC"))
            {
                query = query.OrderBy(p => p.TieuDe);
            }
            else if (sorting.Equals("TieuDe DESC"))
            {
                query = query.OrderByDescending(p => p.TieuDe);
            }
            else
            {
                //mặc định theo thời gian
                query = query.OrderByDescending(p => p.NgayDang);
            }

            List<DTO.DTO> kq = new List<DTO.DTO>();
            if (true)
            {
                if (count > 0)
                {
                    foreach (TINTUC item in query.Skip(startIndex).Take(count).ToList())
                    {
                        DTO.TINTUC_DTO rv = new DTO.TINTUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
                else
                {
                    foreach (TINTUC item in query.ToList())
                    {
                        DTO.TINTUC_DTO rv = new DTO.TINTUC_DTO();
                        rv.fromObjectDatabase(item);
                        kq.Add(rv);
                    }
                }
            }

            return kq;
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isLayXoa)
        {
            IEnumerable<TINTUC> query;
            if (isLayXoa)
            {
                query = DAOUtil.layContext().TINTUCs.AsEnumerable();
            }
            else
            {
                query = DAOUtil.layContext().TINTUCs.Where(e=>e.Xoa ==false).AsEnumerable();
            }
            return LayDanhSachCoThamSo(startIndex, count, sorting, query);
        }

    }

/*
    public class DANHMUCDOANHNGHIEP_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            var kq = DAOUtil.layContext().DOANHNGHIEPs.Select(e => new { DisplayText = e.TenCuaHang, Value = e.ID }).ToList();
            return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
            // DANHMUCCANMUA x = DAOUtil.layContext().DANHMUCCANMUAs.FirstOrDefault(a => a.ID == ID);
            DTO.DANHMUCDOANHNGHIEP_DTO ob = new DTO.DANHMUCDOANHNGHIEP_DTO();
            //ob.fromObjectDatabase(x);

            return ob;
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.DANHMUCDOANHNGHIEP_DTO ob = (DTO.DANHMUCDOANHNGHIEP_DTO)d;
            DANHMUCDOANHNGHIEP i = ob.toObjectDatabase();
            DAOUtil.layContext().DANHMUCDOANHNGHIEPs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            DANHMUCDOANHNGHIEP x = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;

            DAOUtil.layContext().SubmitChanges();

        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.DANHMUCDOANHNGHIEP_DTO ob = (DTO.DANHMUCDOANHNGHIEP_DTO)d;

            DANHMUCDOANHNGHIEP x = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.FirstOrDefault(a => a.ID == ob.ID);
            x.IDDanhMuc = ob.IDDanhMuc;
            x.IDDoanhNghiep = ob.IDDoanhNghiep;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<DANHMUCDOANHNGHIEP> x = DAOUtil.layContext().DANHMUCDOANHNGHIEPs.ToList<DANHMUCDOANHNGHIEP>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.DANHMUCDOANHNGHIEP_DTO xlq = new DTO.DANHMUCDOANHNGHIEP_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new Exception();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }
    */
    public class PHANQUYEN_DAO : DAO
    {
        public List<DTO.DTO> LayDanhSach(int loaiTaiKhoan)
        {
            List<DTO.DTO> kq = new List<DTO.DTO>();
            List<PHANQUYEN> x = DAOUtil.layContext().PHANQUYENs.Where(e => e.IDLoaiTaiKhoan == loaiTaiKhoan && e.Xoa == false).ToList<PHANQUYEN>();
            for (int i = 0; i < x.Count; i++)
            {
                DTO.PHANQUYEN_DTO xlq = new DTO.PHANQUYEN_DTO();
                xlq.fromObjectDatabase(x[i]);
                kq.Add(xlq);
            }
            return kq;
        }

        public List<int> LayDanhSachPhanQuyenTuNguoiDung(int idTaiKhoan)
        {
            List<int> kq = new List<int>();
            TAIKHOAN tk = DAOUtil.layContext().TAIKHOANs.FirstOrDefault(e => e.ID == idTaiKhoan);
            List<PHANQUYEN> x = tk.LOAITAIKHOAN.PHANQUYENs.Where(e =>  e.Xoa == false).ToList<PHANQUYEN>();

            for (int i = 0; i < x.Count; i++)
            {
                kq.Add(x[i].IDQuyen);
            }

            return kq;
        }

        public Object LayDanhSachOptions()
        {
            throw new NotImplementedException();
        }

        public DTO.DTO LayTuID(int ID)
        {
            throw new NotImplementedException();
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.PHANQUYEN_DTO ob = (DTO.PHANQUYEN_DTO)d;
            PHANQUYEN i = ob.toObjectDatabase();
            i.LOAITAIKHOAN = DAOUtil.layContext().LOAITAIKHOANs.FirstOrDefault(e=>e.ID == i.IDLoaiTaiKhoan);
            i.QUYEN = DAOUtil.layContext().QUYENs.FirstOrDefault(e => e.ID == i.IDQuyen);
            DAOUtil.layContext().PHANQUYENs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            PHANQUYEN x = DAOUtil.layContext().PHANQUYENs.FirstOrDefault(a => a.ID == id);
            x.Xoa = !x.Xoa;
            DAOUtil.layContext().SubmitChanges();

        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.PHANQUYEN_DTO ob = (DTO.PHANQUYEN_DTO)d;
            PHANQUYEN x = DAOUtil.layContext().PHANQUYENs.FirstOrDefault(a => a.ID == ob.ID);
            //x.IDLoaiTaiKhoan = ob.IDLoaiTaiKhoan;
            x.IDQuyen = ob.IDQuyen;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.PHANQUYEN_DTO> LayDanhSachPhanQuyenTuLoaiLoaiTK(int loaiTaiKhoan)
        {
            List<DTO.PHANQUYEN_DTO> kq = new List<DTO.PHANQUYEN_DTO>();

            List<PHANQUYEN> x = DAOUtil.layContext().PHANQUYENs.Where(e => e.IDLoaiTaiKhoan == loaiTaiKhoan).ToList<PHANQUYEN>();

            for (int i = 0; i < x.Count; i++)
            {
                DTO.PHANQUYEN_DTO ob = new DTO.PHANQUYEN_DTO();
                ob.fromObjectDatabase(x[i]);
                kq.Add(ob);
            }
            return kq;
        }



        public Object LayDanhSachQuyenOptionTuLoaiTK(int loaiTaiKhoan)
        {
            var kq = DAOUtil.layContext().QUYENs.Select(e => new { DisplayText = e.TenQuyen, Value = e.ID }).ToList();
            return kq;
        }
        public int LaySoLuong(int loaiTaiKhoan)
        {
            return DAOUtil.layContext().PHANQUYENs.Count(e=>e.IDLoaiTaiKhoan == loaiTaiKhoan);
        }
    }

    public class QUYEN_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            var kq = DAOUtil.layContext().QUYENs.Select(e => new { DisplayText = e.TenQuyen, Value = e.ID }).ToList();
            return kq;
        }

        public DTO.DTO LayTuID(int ID)
        {
            throw new NotImplementedException();
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            throw new NotImplementedException();
        }

        public void Xoa(int id)
        {
            throw new NotImplementedException();

        }

        public void CapNhat(DTO.DTO d)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }



    public class TIEUCHUANTIMMUA_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            throw new NotImplementedException();
        }
        
        public DTO.DTO LayTuID(int ID)
        {
            throw new NotImplementedException();
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.TIEUCHUANTIMMUA_DTO ob = (DTO.TIEUCHUANTIMMUA_DTO)d;
            TIEUCHUANTIMMUA i = ob.toObjectDatabase();
            i.TIEUCHUANTHIETBI = DAOUtil.layContext().TIEUCHUANTHIETBIs.FirstOrDefault(e => e.ID == i.IDTieuChuan);
            i.THONGTINTIMMUA = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(e => e.ID == i.IDTimMua);
            DAOUtil.layContext().TIEUCHUANTIMMUAs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            TIEUCHUANTIMMUA x = DAOUtil.layContext().TIEUCHUANTIMMUAs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;

            DAOUtil.layContext().SubmitChanges();

        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.TIEUCHUANTIMMUA_DTO ob = (DTO.TIEUCHUANTIMMUA_DTO)d;
            TIEUCHUANTIMMUA x = DAOUtil.layContext().TIEUCHUANTIMMUAs.FirstOrDefault(a => a.ID == ob.ID);
            //x.IDLoaiTaiKhoan = ob.IDLoaiTaiKhoan;
            //x.IDQuyen = ob.IDQuyen;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }

    public class CHUYENGIAOTIMMUA_DAO : DAO
    {
        public Object LayDanhSachOptions()
        {
            throw new NotImplementedException();
        }

        public DTO.DTO LayTuID(int ID)
        {
            throw new NotImplementedException();
        }

        public DTO.DTO Them(DTO.DTO d)
        {
            DTO.CHUYENGIAOTIMMUA_DTO ob = (DTO.CHUYENGIAOTIMMUA_DTO)d;
            CHUYENGIAOTIMMUA i = ob.toObjectDatabase();
            i.CHUYENGIAO = DAOUtil.layContext().CHUYENGIAOs.FirstOrDefault(e => e.ID == i.IDChuyenGiao);
            i.THONGTINTIMMUA = DAOUtil.layContext().THONGTINTIMMUAs.FirstOrDefault(e => e.ID == i.IDTimMua);
            DAOUtil.layContext().CHUYENGIAOTIMMUAs.InsertOnSubmit(i);
            DAOUtil.layContext().SubmitChanges();
            ob.fromObjectDatabase(i);
            return ob;
        }

        public void Xoa(int id)
        {
            CHUYENGIAOTIMMUA x = DAOUtil.layContext().CHUYENGIAOTIMMUAs.FirstOrDefault(a => a.ID == id);
            x.Xoa = true;

            DAOUtil.layContext().SubmitChanges();

        }

        public void CapNhat(DTO.DTO d)
        {
            DTO.CHUYENGIAOTIMMUA_DTO ob = (DTO.CHUYENGIAOTIMMUA_DTO)d;
            CHUYENGIAOTIMMUA x = DAOUtil.layContext().CHUYENGIAOTIMMUAs.FirstOrDefault(a => a.ID == ob.ID);
            //x.IDLoaiTaiKhoan = ob.IDLoaiTaiKhoan;
            //x.IDQuyen = ob.IDQuyen;
            x.Xoa = ob.Xoa;
            DAOUtil.layContext().SubmitChanges();
        }

        public List<DTO.DTO> LayDanhSach()
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachSuDungLoc(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong()
        {
            throw new NotImplementedException();
        }

        public int LaySoLuong(bool isXoa)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting)
        {
            throw new NotImplementedException();
        }

        public List<DTO.DTO> LayDanhSachCoThamSo(int startIndex, int count, string sorting, bool isXoa)
        {
            throw new NotImplementedException();
        }
    }
}
